[英]How I should get mean and standard deviation depens on "Day" and "Category" columns without filtering manually in the R code
I am getting Mean and standard deviation while filtering the below-mentioned columns in the R console, but how I should get the mean and SD without filtering the columns "Day" and "Category" without filtering it manually in the code and the result should print in the same CSV itself.我在过滤 R 控制台中的以下列时得到平均值和标准偏差,但是如果不过滤列“日”和“类别”而不在代码中手动过滤,我应该如何获得平均值和标准差,结果应该打印在同一个 CSV 本身。
code should automatically create the depends on the column filter like below and print the results in the same CSV:代码应自动创建依赖列过滤器,如下所示,并在相同的 CSV 中打印结果:
mean_Day1_Category_a, SD_Day1_Category_a,
mean_Day4_Category_b, SD_Day4_Category_b, etc...
data数据
Sex F_category Value Day category
M Food 25.6 1 a
M Water 22 2 a
M Food 22 11 a
M Food 24.3 4 b
M Food 24 5 b
F Water 2.03 10 b
code代码
library(dplyr)
library(plyr)
library(doBy)
data <- read.csv("C:/Users/food.csv")
print(data)
data_male<- data %>%
filter(sex == "M")%>%
filter(Day == 1)%>%
filter(F_Category =="FC")%>%
filter(Category =="a")
data_male
sd(data_male$value)
mean(data_male$value)
You have to group the data by day and category instead of filtering.您必须按日期和类别对数据进行分组,而不是过滤。
data_male %>% group_by(Day, Category) %>% summarise(avg_val = mean(Value), std_val = std(Value))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.