2013-06-18 48 views
2

我有一個像如何正確地實現用戶自定義的聚合函數dcast

df<-data.frame(date=c(rep("1/27/2010",times=30)), 
      loc1=c(rep(9:13,each=6)), 
      loc2=c(rep(c("N","E","W"),each=2)), 
      loc3=c(rep(c(1,2))), 
      tr1=c(rep(c(0,1),each=15)), 
      tr2=c(0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1), 
      tr3=c(1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4), 
      Birth=c(sample(c("early","late"),30,replace=TRUE,prob=c(0.5,0.5))), 
      Species=c(rep(c("A","B"),times=15)), 
      Status=c(sample(c(0,1),30,replace=TRUE,prob=c(0.7,0.3)))) 

df<-rbind(df,df) 

我想要單獨列中Loc3的每個值,與LOC1定義行的數據幀,LOC2,TR1,TR2 ,tr3,出生和物種。我想對所有共享這些值的觀察值的狀態進行「計數」,然後按loc3對這些計數進行分組。

我打算使用reshape2軟件包中的dcast。

我寫了一個函數來執行'count'我想要的。我是R新手,雖然我確信有這樣一個功能,但我無法立即找到它,並且考慮到任務的簡單性,嘗試自己編寫腳本似乎是一個值得的練習。

 d.count<-function(x){ 
    j=0 
    for (i in 1:length(x)) 
    if (is.na(x{i])){ 
     j<-j+0 
    }else if(x[i]==0){ 
     j<-j+1 
    } else if(x[i]==1){ 
     j<-j+0 
    } 
    return(j) 
} 

0應增加計數和1和NAS不應該。

所以

​​

我得到的錯誤

Error in if (is.na(x[i])) { : argument is of length zero 

這讓我覺得我不明白怎麼dcast是治療fun.aggregate ...

感謝您的幫助! -JJE

回答

2

爲什麼不能像這樣使用tabulate功能

require(reshape2) 
dcast(df, ... ~ loc3, value.var = "Status", fun.aggregate = tabulate) 

##   date loc1 loc2 tr1 tr2 tr3 Birth Species 1 2 
## 1 1/27/2010 9 E 0 0 1 early  A 0 0 
## 2 1/27/2010 9 E 0 0 1 early  B 0 0 
## 3 1/27/2010 9 N 0 0 1 early  B 0 0 
## 4 1/27/2010 9 N 0 0 1 late  A 0 0 
## 5 1/27/2010 9 W 0 0 1 early  B 0 0 
## 6 1/27/2010 9 W 0 0 1 late  A 0 0 
## 7 1/27/2010 10 E 0 1 2 late  A 0 0 
## 8 1/27/2010 10 E 0 1 2 late  B 0 2 
## 9 1/27/2010 10 N 0 0 1 late  A 0 0 
## 10 1/27/2010 10 N 0 1 2 late  B 0 2 
## 11 1/27/2010 10 W 0 1 2 late  A 0 0 
## 12 1/27/2010 10 W 0 1 2 late  B 0 0 
## 13 1/27/2010 11 E 0 1 2 late  A 0 0 
## 14 1/27/2010 11 E 1 0 3 early  B 0 2 
## 15 1/27/2010 11 N 0 1 2 early  B 0 0 
## 16 1/27/2010 11 N 0 1 2 late  A 0 0 
## 17 1/27/2010 11 W 1 0 3 late  A 0 0 
## 18 1/27/2010 11 W 1 0 3 late  B 0 2 
## 19 1/27/2010 12 E 1 0 3 early  B 0 0 
## 20 1/27/2010 12 E 1 0 3 late  A 0 0 
## 21 1/27/2010 12 N 1 0 3 early  A 2 0 
## 22 1/27/2010 12 N 1 0 3 early  B 0 2 
## 23 1/27/2010 12 W 1 0 4 early  A 0 0 
## 24 1/27/2010 12 W 1 1 4 early  B 0 0 
## 25 1/27/2010 13 E 1 1 4 early  B 0 0 
## 26 1/27/2010 13 E 1 1 4 late  A 0 0 
## 27 1/27/2010 13 N 1 1 4 late  A 0 0 
## 28 1/27/2010 13 N 1 1 4 late  B 0 2 
## 29 1/27/2010 13 W 1 1 4 early  A 0 0 
## 30 1/27/2010 13 W 1 1 4 early  B 0 2 

編輯

如果你要計算的0例如數量:

dcast(df, ... ~ loc3, value.var = "Status", 
     fun.aggregate = function(x) sum(x == 0, na.rm = TRUE)) 
+0

哇,這似乎來過我的問題,謝謝。我仍然不明白dcast是如何處理fun.aggregate參數的。另外我如何確保列表計數零?在矢量上運行製表符,給我一個向量,並且dcast需要fun.aggregate參數來給出一個數字。我如何計算1的表格(我需要做下一步...) – user2498270

+0

@ user2498270如果你需要一個函數來計算0的數量,例如你可以做一些像'dcount < - function(x)sum (x == 0,na.rm = TRUE)'。你可以用它來代替'tabulate',如果你想計算1的數量,那麼只需用'sum(x == 1)'替換'sum(x == 0)'。希望它有幫助 – dickoa

+0

這正是我需要的!再次感謝! – user2498270

相關問題