首页 > 解决方案 > 在 r 中填充一个 4-D 数组

问题描述

我正在尝试将数据加载到 4-D 数组中并且遇到了一些麻烦。这是数据的结构和我正在使用的代码:

  X   CREPid Year Survey Habitat julien Clouds Precip Noise Wind y species
1 1 19990008 2012      1       1    151      1      1     1    1 1    AMGO
2 2 19990027 2012      1       2    173      3      1     1    1 1    AMGO
3 3 19990031 2012      1       1    139      1      1     2    3 0    AMGO
4 4 19990033 2012      1       1    181      3      1     1    1 1    AMGO
5 5 19990034 2012      1       1    181      2      1     2    2 1    AMGO
6 6 19990037 2012      1       1    139      1      1     1    1 1    AMGO

# Format data in a spreadsheet format into a 4d array
# Determine required dimensions of 4d array
nsite <- length(unique(df$site))    # Number of sites (242)
nvisit <- length(unique(df$visit))  # Number of surveys or visits(3 per year)
nyear <- length(unique(df$year))    # Number of years(4 years)
nspec <- length(unique(df$species)) # Number of species (46 species)

# Prepare array and pre-fill array with NAs
BigArray2 <- array(NA, dim = c(nsite, nvisit, nyear, nspec))

# Fill array with the detection/nondetection data
# Loop over all rows in the spreadsheet data and fill them in 
#   at the right place in the 4d array

for(i in 1:nrow(CREPallOccFinal)){
BigArray2[CREPallOccFinal$CREPid[i], CREPallOccFinal$Survey[i], CREPallOccFinal$Year[i], CREPallOccFinal$species[i]]<- CREPallOccFinal$y[i]}

但是,每次运行代码时,都会收到以下错误消息:

[<-( , CREPallOccFinal *tmp*$CREPid[i], CREPallOccFinal$Survey[i], 中的错误:下标越界

我无法弄清楚问题,任何帮助将不胜感激。

标签: rmultidimensional-array

解决方案


您使用了超出允许范围的索引。例如,对于 CREPid,您使用 19990008,但它只能在 1-242 的范围内。下面显示了如何创建阵列的示例。

首先用 提取所有级别的键unique。然后像你一样创建你的数组,但我也定义了dimnames,它允许按名称访问。为了获取数组match的索引,我使用它返回找到给定值的索引。

#Creaete data
set.seed(7)
n  <- 1e4
x  <- data.frame(id=sample(10:252, n, TRUE), survey=sample(1:3, n, TRUE)
  , year=sample(2012:2015, n, TRUE), species=sample(letters, n, TRUE)
  , y=runif(n, 0, 3))
head(x)
#   id survey year species         y
#1  51      2 2014       o 1.0825328
#2 220      3 2015       f 2.1976835
#3 168      2 2014       f 0.9047656
#4 229      3 2012       t 2.1546260
#5 112      3 2015       c 2.8865230
#6 203      1 2013       l 1.4364280

#Unique levels of keys
tid  <- unique(x$id)
tsurvey  <- unique(x$survey)
tyear  <- unique(x$year)
tspecies  <- unique(x$species)

#Create array
arr <- array(NA
  , dim = c(length(tid), length(tsurvey), length(tyear), length(tspecies))
  , dimnames = list(tid, tsurvey, tyear, tspecies))

#Fill array
arr[matrix(c(match(x$id,tid), match(x$survey,tsurvey), match(x$year,tyear)
  , match(x$species,tspecies)), ncol=4)] <- x$y

#Get from array by name
arr["51","2","2014","o"]
#[1] 1.082533

推荐阅读