# Error in colSums(iris) : ‘x‘ must be numeric,# Error in rowSums(iris) : ‘x‘ must be numeric

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了# Error in colSums(iris) : ‘x‘ must be numeric,# Error in rowSums(iris) : ‘x‘ must be numeric相关的知识,希望对你有一定的参考价值。

# Error in colSums(iris) : 'x' must be numeric,# Error in rowSums(iris) : 'x' must be numeric,# Error in colMeans(iris) : 'x' must be numeric,# Error in rowMeans(iris) : 'x' must be numeric

目录

# Error in colSums(iris) : 'x' must be numeric,# Error in rowSums(iris) : 'x' must be numeric,# Error in colMeans(iris) : 'x' must be numeric,# Error in rowMeans(iris) : 'x' must be numeric

问题:

解决:

完整错误:


问题:

data(iris)                                                  
# Load iris data
head(iris)                                                  
# First 6 rows of iris data

colSums(iris)                                               

# colSums error
# Error in colSums(iris) : 'x' must be numeric

rowSums(iris)                                               

# rowSums error
# Error in rowSums(iris) : 'x' must be numeric

colMeans(iris)                                              
# colMeans error
# Error in colMeans(iris) : 'x' must be numeric

rowMeans(iris)                                              
# rowMeans error
# Error in rowMeans(iris) : 'x' must be numeric

解决:

# 获取dataframe中的数值列

iris_subset <- unlist(lapply(iris, is.numeric))  
           
# Subset containing numeric columns
iris_subset
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#         TRUE         TRUE         TRUE         TRUE        FALSE

# 不是数值型当然没法计算

colSums(iris[ , iris_subset])                                

# No colSums error anymore
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#        876.5        458.6        563.7        179.9

rowSums(iris[ , iris_subset])                                
# No rowSums error anymore
# 10.2  9.5  9.4  9.4 10.2 11.4  9.7 10.1  8.9  9.6...

colMeans(iris[ , iris_subset])                               
# No colMeans error anymore
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#     5.843333     3.057333     3.758000     1.199333

rowMeans(iris[ , iris_subset])                               
# No rowMeans error anymore
# 2.550 2.375 2.350 2.350 2.550 2.850 2.425 2.525...

 

完整错误:

# Error in colSums(iris) : 'x' must be numeric
# Error in rowSums(iris) : 'x' must be numeric
# Error in colMeans(iris) : 'x' must be numeric
# Error in rowMeans(iris) : 'x' must be numeric

......

以上是关于# Error in colSums(iris) : ‘x‘ must be numeric,# Error in rowSums(iris) : ‘x‘ must be numeric的主要内容,如果未能解决你的问题,请参考以下文章

R语言colSums函数rowSums函数colMeans函数rowMeans函数colMedians函数rowMedians计算dataframe行或者列的加和均值中位数实战

apply.weeky函数突然返回“'x'必须是数字”,仅当使用“ sum”或“ colSums”时

sklearn学习随笔1

sklearn基础知识-准备阶段

如何在 r 中使用 VIF?

[RxJS] Error Handling in RxJS