1

我有一个数据集raw.data.2010,需要对不同动物物种进行子集的几个步骤。我还需要在每个过滤过程之后相应地命名它们。我写了一个简单的代码如下:

#Creating reproducible data######
site=rep(list("Q", "R", "S", "T"), each=500)
grid=sample(1:2, size=2000, replace=TRUE)
spp=rep(list("A", "B", "C", "D", "E"), each=400)
fate=sample(1:5, size=20000, replace=TRUE)
sex=rep(list("M","F"), each=2000)
weight=sample(85:140, size=2000, replace=TRUE)

raw.data=as.data.frame(cbind(site, grid, spp, fate, sex, weight))

### main codes#####
spp=c("A", "B", "C", "D", "E")
    for (i in spp){
        name=paste(i, "raw", sep=".", collapse="")
        filter=paste("select",i, sep="", collapse="")
        assign(filter, raw.data$spp==i)
        assign(name, raw.data[get(filter),])
    }

我检查了过滤器,它们正常工作。但是最后一行不起作用,所以我调用的所有子集数据都返回了NA。什么问题?谢谢你。

编辑:嗨,谢谢大家的建议。我编辑了我的代码,所以它是可重现的。基本上我想先raw.dataspp. 然后我可以继续添加更多过滤器以根据site, grid, fate... 等对它们进行分组。我需要能够单独访问过滤后的数据,以便我可以操作它们以供以后使用,例如。计算不同性别或年龄组的体重和其他测量值。A.raw我希望以后能打电话A.Q.data

因为我想在不同级别(例如人口级别、个人级别、站点/网格级别)分析我的数据,并能够根据我的需要合并/拆分它们。这就是这段代码的目的。希望我的解释不会让你感到困惑。

4

3 回答 3

8

assign从长远来看,如果您不再使用和使用全局变量,get而是使用列表(并记住使用[[而不是子集) ,那么从长远来看,您可能会为自己节省很多工作和悲伤$

于 2012-09-08T18:42:04.877 回答
3

问题似乎是您需要“获取”具有存储在过滤器中的名称的变量,而不是使用过滤器本身。

这应该有效:

spp=c("A", "B", "C", "D", "E")
for (i in spp){
    name=paste(i, "raw", sep=".", collapse="")
    filter=paste("select",i, sep="", collapse="")
    assign(filter, raw.data.2010$Spp==i)
    assign(name, raw.data.2010[get(filter),])
}
于 2012-09-08T18:33:39.840 回答
0

你的例子都搞砸了。这是一个正确的例子,永远不会使用as.data.frame(cbind(...))

 site=rep(c("Q", "R", "S", "T"), each=500)
 grid=sample(1:2, size=2000, replace=TRUE)
 spp=rep(c("A", "B", "C", "D", "E"), each=400)
 fate=sample(1:5, size=20000, replace=TRUE)
 sex=rep(c("M","F"), each=2000)
 weight=sample(85:140, size=2000, replace=TRUE)

 raw.data=data.frame(site=site, grid=grid, spp=spp, fate=fate, sex=sex, weight=weight)
 names(group.spp) <- paste(levels(raw.data$spp), "raw", sep=".")

#------------------------
 str(group.spp)
List of 5
 $ A.raw:'data.frame':  4000 obs. of  6 variables:
  ..$ site  : Factor w/ 4 levels "Q","R","S","T": 1 1 1 1 1 1 1 1 1 1 ...
  ..$ grid  : int [1:4000] 2 1 2 1 2 1 1 1 1 2 ...
  ..$ spp   : Factor w/ 5 levels "A","B","C","D",..: 1 1 1 1 1 1 1 1 1 1 ...
  ..$ fate  : int [1:4000] 3 2 3 5 5 2 3 2 5 2 ...
  ..$ sex   : Factor w/ 2 levels "F","M": 2 2 2 2 2 2 2 2 2 2 ...
  ..$ weight: int [1:4000] 136 93 115 100 97 128 120 124 97 120 ...
 $ B.raw:'data.frame':  4000 obs. of  6 variables:
  ..$ site  : Factor w/ 4 levels "Q","R","S","T": 1 1 1 1 1 1 1 1 1 1 ...
  ..$ grid  : int [1:4000] 2 2 1 2 2 2 1 2 2 2 ...
  ..$ spp   : Factor w/ 5 levels "A","B","C","D",..: 2 2 2 2 2 2 2 2 2 2 ...
  ..$ fate  : int [1:4000] 5 5 2 4 3 4 2 3 4 5 ...
  ..$ sex   : Factor w/ 2 levels "F","M": 2 2 2 2 2 2 2 2 2 2 ...
  ..$ weight: int [1:4000] 137 126 116 97 97 86 134 103 86 140 ...
 $ C.raw:'data.frame':  4000 obs. of  6 variables:
  ..$ site  : Factor w/ 4 levels "Q","R","S","T": 2 2 2 2 2 2 2 2 2 2 ...
  ..$ grid  : int [1:4000] 1 2 1 2 2 2 1 2 2 1 ...
  ..$ spp   : Factor w/ 5 levels "A","B","C","D",..: 3 3 3 3 3 3 3 3 3 3 ...
  ..$ fate  : int [1:4000] 2 4 4 2 5 1 2 1 2 5 ...
  ..$ sex   : Factor w/ 2 levels "F","M": 2 2 2 2 2 2 2 2 2 2 ...
  ..$ weight: int [1:4000] 132 85 96 87 91 94 94 122 116 87 ...
 $ D.raw:'data.frame':  4000 obs. of  6 variables:
  ..$ site  : Factor w/ 4 levels "Q","R","S","T": 3 3 3 3 3 3 3 3 3 3 ...
  ..$ grid  : int [1:4000] 2 2 2 1 1 2 2 1 1 2 ...
  ..$ spp   : Factor w/ 5 levels "A","B","C","D",..: 4 4 4 4 4 4 4 4 4 4 ...
  ..$ fate  : int [1:4000] 2 4 1 4 2 4 1 5 1 4 ...
  ..$ sex   : Factor w/ 2 levels "F","M": 2 2 2 2 2 2 2 2 2 2 ...
  ..$ weight: int [1:4000] 130 139 100 107 126 119 134 110 103 135 ...
 $ E.raw:'data.frame':  4000 obs. of  6 variables:
  ..$ site  : Factor w/ 4 levels "Q","R","S","T": 4 4 4 4 4 4 4 4 4 4 ...
  ..$ grid  : int [1:4000] 2 2 1 1 1 1 2 2 2 1 ...
  ..$ spp   : Factor w/ 5 levels "A","B","C","D",..: 5 5 5 5 5 5 5 5 5 5 ...
  ..$ fate  : int [1:4000] 5 5 4 5 5 3 1 4 4 3 ...
  ..$ sex   : Factor w/ 2 levels "F","M": 2 2 2 2 2 2 2 2 2 2 ...
  ..$ weight: int [1:4000] 88 96 99 101 119 94 97 132 137 115 ...
于 2012-09-09T02:40:37.140 回答