简体   繁体   English

在一系列列上使用copy()并将统计信息应用于结果

[英]Use replicate() on a range of columns and apply stats to the results

I've got a cross-tab of frequencies ie for column A01 there are 6485 counts of 13 CAGs, 35 counts of 14 CAGs etc. I've managed to convert it into long data for column A01, which I can then use to work out mean and sd. 我有一个频率交叉表,即对于A01列,有6485个计数为13个CAG,35个计数为14个CAG等。我设法将其转换为A01列的长数据,然后我可以使用它来工作出平均值和标准偏差。 However, I've got n columns, and I'm not sure how to apply this function to all columns. 但是,我有n列,而且我不确定如何将此函数应用于所有列。

data <- data.frame(CAG = c(13, 14, 15), A01 = c(6485,35,132), A02 = c(0,42,56))
cag = rep(data$CAG, data$A01)
mean <- mean(cag)
stdef <- sd(cag)
L = list()

for (i in 1:ncol(data)){
  tmp = rep(data[i, 1], data[i, 2])
  mean = mean(tmp)
  stdef = sd(tmp)
  L[[i]] = cbind(mean, stdef)
}
Mx = do.call(rbind, L)
Mx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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