Merge 4 data objects with different columns (variables) in R

前端 未结 2 708
鱼传尺愫
鱼传尺愫 2021-01-23 12:37

So initially I had the following object:

> head(gs)
  year disturbance lek_id  complex tot_male
1 2006           N     3T  Diamond        3
2 2007           N         


        
相关标签:
2条回答
  • 2021-01-23 12:57
    mystat <- function(x) c(mi=min(x), ma=max(x))
    aggregate(Sepal.Length~Species, FUN=mystat, data=iris)
    

    for you:

    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)
    
    0 讨论(0)
  • 2021-01-23 13:05

    This might work (but hard to check without a reproducible example):

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

    But instead of aggregating for the separate statistics and then merging, you can also aggregate everything at once into a new dataframe / datatable with for example data.table, dplyr or base R. Then you don't have to merge afterwards (for a base R solution see the other answer):

    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 讨论(0)
提交回复
热议问题