r - How to calculate rowMeans for dataframe? -
i trying calculate rowmeans dataframe using these command
stamrow <- data.frame(probeid=stam[,1], means=rowmeans(stam[,c(-1,-2,-3)])) stam$means <- rowmeans(stam[,-(1:3)])
but getting error in both case
rowmeans(stam[, -(1:3)]) : 'x' must numeric
my dataset this:
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
thanking in advance
it works data posted. check str(stam)
see if columns numeric
.
data.frame(probeid=stam[,1], means=rowmeans(stam[,-c(1:3)])) # probeid means #1 chr10fs00300029 0.4384 #2 chr10fs003018825 0.3464
update
if numeric
columns start 4
15
, can convert columns factor
class numeric
first
stam[,4:15] <- lapply(stam[,4:15], function(x) as.numeric(as.character(x)))
and apply rowmeans
or can specify numeric
in colclasses
argument in read.table
stam <- read.table('file.txt', header=true, sep='', colclasses=c(rep('character',3), rep('numeric',12)))
data
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))
Comments
Post a Comment