简体   繁体   中英

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. However, I've got n columns, and I'm not sure how to apply this function to all columns.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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