2012-09-08 17 views
1

我有一個數據集raw.data.2010需要幾個步驟與不同動物物種進行子集化。我也需要在每個過濾過程後對其進行相應的命名。我寫了一個簡單的代碼如下:R,用字符過濾(子集)數據並相應地分配名稱?

#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。什麼問題?謝謝。

編輯:你好,謝謝大家的意見。我編輯了我的代碼,使其具有可重複性。基本上我想先用spp過濾我的raw.data。然後,我可以繼續添加更多的過濾器,按照site,grid,fate等分組。我需要能夠單獨訪問過濾的數據,以便我可以操作它們以備後用,例如。計算不同性別或年齡組的體重和其他測量值。我希望能夠稍後致電A.rawA.Q.data。根據我的需求

因爲我想分析我的數據在不同層次(如人口水平,個人層面上,網站/格水平),並且能夠彙集/分割。這是此代碼的目的。希望我的解釋不會混淆你。

+3

如果你犯了一個[重複的例子(http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-可重現的例子),這表明你的問題/問題,我們會發現它更容易回答。 – Andrie

+3

我強烈懷疑你會更喜歡做'split(raw.data.2010,raw.data.2010 $ Spp)' - 但我強烈支持@ Andrie對可複製數據的建議,以及一點點更多的上下文:一旦你創建它們,你將如何處理派生變量? –

+0

我通過添加可重現的數據框來編輯我的代碼,並且還添加了我的研究目的的更多解釋。 – lamushidi

回答

8

你可能會節省大量的工作和悲傷的,從長遠來看,如果你使用全局變量assignget,而是搬走與列表工作(記住使用[[代替$)。

+2

不能同意更多。 –

+0

哦,用[[快於$?對不起,如果對大家都是常識,我根本不是程序員,還在學習。 – lamushidi

+1

這不是說「[[」更快,更安全,更靈活。它評估了「$」沒有的論證。您需要評估過濾器對象(順便提一下,名字很可怕)。 –

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),]) 
} 
+0

我明白了!非常感謝!! – lamushidi

+1

請看@格雷格斯諾的回答。一般來說,使用get和assign不是一個好主意。改用列表。 – mkayala

+0

'崩潰'的論點是愚蠢的。 –

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 ... 
相關問題