【问题标题】:Why does R add an "x" when renaming raster stack layers为什么R在重命名栅格堆栈图层时会添加“x”
【发布时间】:2016-08-19 01:45:54
【问题描述】:

我在 R 中有一个包含 84 层的栅格堆栈/砖,我试图根据 199911 到 200610(1999 年 11 月到 2006 年 10 月)的年份和月份来命名它们。但是由于某种原因,R 不断在我给我的图层命名的任何名称的开头添加一个“X”。

有谁知道为什么会发生这种情况以及如何解决它?以下是我尝试过的一些方法:

# Import raster brick

rast <- brick("rast.tif")

names(rast)[1:3]

[1] "MonthlyRainfall.1" "MonthlyRainfall.2" "MonthlyRainfall.3"

## Method 1

names(rast) <- paste0(rep(1999:2006, each=12), 1:12)[11:94]
names(rast)[1:3]

[1] "X199911" "X199912" "X20001" 

## Method 2

# Create a vector of dates

dates <- format(seq(as.Date('1999/11/1'), as.Date('2006/10/1'), by='month'), '%Y%m')
dates[1:3]

[1] "199911" "199912" "200001"

# Set names

rast <- setNames(rast, dates)
names(rast)[1:3]

[1] "X199911" "X199912" "X200001"

## Method 3

names(rast) <- paste0("", dates)
names(rast)[1:3]

[1] "X199911" "X199912" "X200001"

## Method 4

substr(names(rast), 2, 7)[1:3]

[1] "199911" "199912" "200001"

names(rast) <- substr(names(rast), 2, 7)
names(rast)[1:3]

[1] "X199911" "X199912" "X200001"

在某种程度上,我已经能够通过在我的一些其他数据的开头添加“X”来解决这个问题,但现在它已经到了我不能再这样做的地步了。任何帮助将不胜感激!

【问题讨论】:

  • 阅读?make.names的文档。

标签: r rename raster r-raster


【解决方案1】:

R 不允许列以数字开头,因此它会在前面添加一个字符以避免该限制。

【讨论】:

  • 谢谢!我已经编辑,所以问题中有一个解决方案并接受了它。
猜你喜欢
  • 2020-06-10
  • 1970-01-01
  • 2014-11-01
  • 2018-07-08
  • 2016-04-19
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多