【发布时间】:2021-01-27 10:33:31
【问题描述】:
幸运的是,分组计算mean和sd的第一步已经完成。现在我分别得到了mean 和sd 结果。而我想做的是如何将主题结合在一起。不管组合方法有多简单或多难,但组合数据框应该简单还是不复杂。
这里我将向您展示我的计算方法和我所知道的唯一组合方法。我需要另一种新的组合方法。请。我的示例数据和代码如下:
data<-data.frame(matrix(sample(1:1000,500),20,25))
names(data) <- c(paste0("Gene_", 1:25))
rownames(data)<-NULL
data$Name<-c(rep(paste0("Group_",1:10),each=2))
unique(data$Name)
## 1 group_by, only get one result each time
mm <- data %>%
group_by(data$Name) %>%
summarise(mean=mean(Gene_1))
mm
## 2 tapply, can get the mean of each column , but only one column each time.
mm <- data.frame(mean_Gene_1=tapply(data[,"Gene_1"],data$Name,mean))
mm
## 3.aggregate, a powerful function , can get all the columns result by group.
mm <- aggregate(.~Name,data,mean)
mm
## get the mean and sd dataframe.
mean <- aggregate(.~Name,data,mean)
sd <- aggregate(.~Name,data,sd)
## now combine the two dataframe usingt the same index "Name" and "gene"
## I just learned one method from somebody in Stack overflow.
## combine the two file
data <- bind_rows(list(mean = mean, sd = sd), .id = "stat")
data_mean_sd <- data %>%
pivot_longer(-c(Name, stat), names_to = "Gene", values_to = "value") %>%
pivot_wider(names_from = "stat", values_from = "value")
你知道结果是对的。但它是一个大文件,虽然它是这里的一个例子。它包括许多重复的数据。我希望有人给我一个更好的方法来简化我的结果。
我需要你的帮助。
【问题讨论】: