【发布时间】:2021-04-13 07:24:27
【问题描述】:
我正在创建 2 个数据框并将它们合并。我在下面创建了重复的代码,因为我是 R 编程新手,所以我想要相同的结果而不创建重复的代码。
category sex day flag value mean Standard deviation
1 FC F -1 a 17.2 17.01333 0.9463212
2 FC F -1 a 17.0 17.01333 0.9463212
3 FC F -1 a 18.7 17.01333 0.9463212
4 FC F -1 a 17.1 17.01333 0.9463212
5 FC F -1 a 17.2 17.01333 0.9463212
6 FC F -1 a 17.2 17.01333 0.9463212
library(dplyr)
library(plyr)
library(doBy)
library(tidyverse)
data <- read.csv("users/study.csv")
print(data)
new_table <- select(data, category, sex, day, flag,value)
target1 <- "a"
target2<-"b"
#Repeated Codes
filtered1<-filter(new_table, sex=="F", category=="FC",flag %in% target1,day==-1)
filtered1
filtered2<-filter(new_table, sex=="F", category=="FC",flag %in% target2,day==-1)
filtered2
result1<-filtered1 %>%
mutate(mean = mean(value),
`Standard deviation` = sd(value))
result2<-filtered2 %>%
mutate(mean = mean(value),
`Standard deviation` = sd(value))
#Merging the dataframes
dataframe<-do.call("rbind", list(result1,result2))
dataframe
【问题讨论】:
-
列
Sex和FC是否分别具有F和FC以外的值? -
是的,它具有 F 和 FC 以外的值,我想过滤此列中的每个值并获取结果。但是由于FC有更多的值,应该如何在过滤时不硬编码值来完成
-
您可能希望对每个此类值组合进行这些计算?
-
是的,你是对的
-
是的,我必须过滤多个组合。例如在“类别”和“标志”中
标签: r