我的R剧本是:
aa <- data.frame("1","3","1.5","2.1")
mean(aa)然后我得到:
1 NA
警告消息:在mean.default(aa)中:参数不是数字或逻辑:返回NA
在不删除引号或将data.frame更改为其他东西的情况下,可以解决这个问题吗?
发布于 2017-09-26 09:04:14
对于一个data.frame,如果您想获得每个列的平均值,可以使用colMeans函数来实现:
aa <- data.frame("1","3","1.5","2.1", stringsAsFactors = FALSE)
aa <- sapply(aa, as.numeric)
colMeans(aa)
mean(colMeans(aa)) # if you want average across all columns您必须使用data.frame创建stringsAsFactors = FALSE,否则所有的字符值都将是单独的因素,每一个都是序数1。每个值的数值表示形式为1。
https://stackoverflow.com/questions/46422089
复制相似问题