简体   繁体   English

在 R 代码中,我应该如何获得均值和标准差取决于“日”和“类别”列而不手动过滤

[英]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.

相关问题 如何通过在 R 中创建额外的列(均值和标准差)来获得相同数据帧的均值和标准差的结果 - How to get the results of a mean and standard deviation to the same data frame by creating extra columns (mean and standard deviation) in R 在 R 中使用手动设置的平均值计算标准偏差 - Compute standard deviation with a manually set mean in R 按类别绘制均值和标准差 - Plot mean and standard deviation by category 如何编码加权二项式分布的均值和标准差? - How can I code the mean and standard deviation for a weighted binomial distribution? 如何计算R中多个标准差的平均值 - How to calculate mean of multiple standard deviation in R 如何找到与 R 代码中的平均值相差一个标准偏差内的数据点(在一列内)的比例? - How do I find proportion of the datapoints (within one column) that are within one standard deviation away from the mean in R code? R:每个受试者的均值,方差和标准差列 - R: mean, variance and standard deviation columns per subject 将某些列重新调整为 R 中的特定平均值和标准差 - Rescaled certain columns to specific mean and standard deviation in R 按组获取标准偏差并从 R 中的平均列标准偏差中减去 - Get a standard deviation by group and subtract from mean column standard deviation in R 如何在R中按组同时计算均值,cv和标准差 - How to compute mean, cv and standard deviation simultaneously using by group in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM