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

前端 未结 2 707
鱼传尺愫
鱼传尺愫 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 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))
    

提交回复
热议问题