2015-10-09 27 views
1

不同的列(變量)數據對象,以便最初我具有下列對象:合併4中的R

> head(gs) 
    year disturbance lek_id complex tot_male 
1 2006   N  3T Diamond  3 
2 2007   N  3T Diamond  17 
3 1981   N bare 3corners  4 
4 1982   N bare 3corners  7 
5 1983   N bare 3corners  2 
6 1985   N bare 3corners  5 

隨着我來計算一般統計最小值,最大值,平均值和SD tot_male的複雜內年。我使用了R數據分割函數,並在它看起來合適的地方分配了邏輯列名稱,最終使它們成爲不同的對象。

> tyc_min = aggregate(gs$tot_male, by=list(gs$year, gs$complex), FUN=min) 
> names(tyc_min) = c("year", "complex", "tot_male_min") 
> tyc_max = aggregate(gs$tot_male, by=list(gs$year, gs$complex), FUN=max) 
> names(tyc_max) = c("year", "complex", "tot_male_max") 
> tyc_mean = aggregate(gs$tot_male, by=list(gs$year, gs$complex), FUN=mean) 
> names(tyc_mean) = c("year", "complex", "tot_male_mean") 
> tyc_sd = aggregate(gs$tot_male, by=list(gs$year, gs$complex), FUN=sd) 
> names(tyc_sd) = c("year", "complex", "tot_male_sd") 

輸出示例(第二對象 - Tyc_max):

year complex tot_male_max 
1 2003      0 
2 1970 3corners   26 
3 1971 3corners   22 
4 1972 3corners   26 
5 1973 3corners   32 
6 1974 3corners   18 

現在我需要添加每年/複雜組合的樣本數爲好。然後,我需要這些合併成單一的數據對象,並導出爲.csv文件

我知道我需要使用合併()函數all.y一起,但不知道如何處理這樣的錯誤:

Error in fix.by(by.x, x) : 
    'by' must specify one or more columns as numbers, names or logical 

或者..每年添加樣本數量和複雜度。有什麼建議麼?

+1

你可以提供一個可重複的例子嗎? –

回答

1

這可能會實現(但很難查無reproducible example):

gsnew <- Reduce(function(...) merge(..., all = TRUE, by = c("year","complex")), 
       list(tyc_min, tyc_max, tyc_mean, tyc_sd)) 

但不是彙總的分類統計,然後合併,你也可以聚集一切一下子進入一個新的數據幀/數據表與例如data.tabledplyr或基地R.然後,你不必事後合併(針對基礎R解決方案看對方的回答):

library(data.table) 
gsnew <- setDT(gs)[, .(male_min = min(tot_male), 
         male_max = max(tot_male), 
         male_mean = mean(tot_male), 
         male_sd = sd(tot_male), by = .(year, complex)] 

library(dplyr) 
gsnew <- gs %>% group_by(year, complex) %>% 
    summarise(male_min = min(tot_male), 
      male_max = max(tot_male), 
      male_mean = mean(tot_male), 
      male_sd = sd(tot_male)) 
+0

dplyr工作得很好。我在彙總調用中添加了n = length(tot_male),並且還爲每個複雜組合每年獲得了多少個樣本。謝謝大家的所有幫助! –

+0

@SpencerHudson而不是'n = length(tot_male)'你也可以使用'n = n()' – Jaap

+0

這太有道理了。謝謝! –

1
mystat <- function(x) c(mi=min(x), ma=max(x)) 
aggregate(Sepal.Length~Species, FUN=mystat, data=iris) 

你:

mystat <- function(x) c(mi=min(x), ma=max(x), m=mean(x), s=sd(x), l=length(x)) 
aggregate(tot_male~year+complex, FUN=mystat, data=gs)