简体   繁体   中英

Calculating mean and standard deviation in R in one column depending on factors in other columns

I would like to calculate the mean and the standard deviation from the data in the "skada" column that are depending in three other columns. My table looks like this: 在此处输入图片说明

The "geografi" column have the categorical variables: SV, NV, M, SO, SV

The "gradering" column have the categorical variables: 1, 2

The "plats" column have the categorical variables: 20m, kant

In other words, this means I would have the mean and standard deviation for SV,1,20m; SV,2,20m; SV,1,kant; SV,2,kant; NV,1,20m,...... and so forth. Does anyone have any tips on how to do this easily?

Cheers!

You can use data.table:

library(data.table) 

setDT(data)[, list(skada_mean = mean(skada), skada_sd = sd(skada)), 
                 by = c("geografi", "gardering", "plats")]

or dyplr:

library(dplyr)

data %>% 
    group_by(geografi, gardering, plats) %>% 
    summarise(skada_mean = mean(value), skada_sd = sd(value))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM