我试图使用这些命令
计算数据帧的rowMeansstamRow <- data.frame(ProbeID=stam[,1], Means=rowMeans(stam[,c(-1,-2,-3)]))
stam$Means <- rowMeans(stam[,-(1:3)])
但是我在这两种情况下都遇到了错误
rowMeans(stam[, -(1:3)]) : 'x' must be numeric
我的数据集看起来像这样:
stam =
ProbeId Chr Position Sample1 Sample2 Sample3 Sample4 Sample5
CHR10FS00300029 10 3000293 0.132 0.135 0.312 0.724 0.889
CHR10FS003018825 10 3018825 0.524 0.446 0.203 -0.022 0.581
提前感谢你
答案 0 :(得分:0)
它适用于您发布的数据。我会检查str(stam)
以查看列是否为numeric
。
data.frame(ProbeID=stam[,1], Means=rowMeans(stam[,-c(1:3)]))
# ProbeID Means
#1 CHR10FS00300029 0.4384
#2 CHR10FS003018825 0.3464
如果numeric
列从4
开始到15
,您可以先将这些列从factor
类转换为numeric
stam[,4:15] <- lapply(stam[,4:15], function(x) as.numeric(as.character(x)))
然后应用rowMeans
或者您可以在numeric
colClasses
参数中指定read.table
stam <- read.table('file.txt', header=TRUE, sep='',
colClasses=c(rep('character',3), rep('numeric',12)))
stam <- structure(list(robeId = c("CHR10FS00300029", "CHR10FS003018825"
), Chr = c(10L, 10L), Position = c(3000293L, 3018825L), Sample1 = c(0.132,
0.524), Sample2 = c(0.135, 0.446), Sample3 = c(0.312, 0.203),
Sample4 = c(0.724, -0.022), Sample5 = c(0.889, 0.581)), .Names = c("robeId",
"Chr", "Position", "Sample1", "Sample2", "Sample3", "Sample4",
"Sample5"), class = "data.frame", row.names = c(NA, -2L))