r - 如何判断数据框的所有 colSums 或 rowSums(或其他聚合)在 R 中是否相等?
问题描述
我不明白这种行为:
> a = c(1,1,1,1,1)
> (a==1)
[1] TRUE TRUE TRUE TRUE TRUE
> df = data.frame(a=c(1,2,3,4,5), b=c(6,7,8,9,10))
> df
a b
1 1 6
2 2 7
3 3 8
4 4 9
5 5 10
> colSums(df)
a b
15 40
> ndf = apply(df, 2, function(col){col/sum(col)})
> colSums(ndf)
a b
1 1
> (colSums(ndf) == 1)
a b
TRUE FALSE
> typeof(a)
[1] "double"
> class(a)
[1] "numeric"
> typeof(colSums(ndf))
[1] "double"
> class(colSums(ndf))
[1] "numeric"
> b = c(1,1,1,2,1)
> all(b==1)
[1] FALSE
> (b==1)
[1] TRUE TRUE TRUE FALSE TRUE
> all(a==1)
[1] TRUE
> all(colSums(ndf)==1)
[1] FALSE
为什么“有效”a
和无效的相同测试b
不起作用colSums(ndf)
?
解决方案
推荐阅读
- reactjs - Ant Design - 垂直的复选框组
- python - 使用 Python 和 Selenium 进行抓取 - 如果元素不存在,我应该如何返回“null”
- excel - Powershell:将日期导入 Excel 更改格式
- kubernetes - Minikub 未启动 Ubuntu 18.4
- javascript - 比较并删除字符串的重复部分
- python-3.x - 如何将函数应用于每组数据框
- go - 您将客户端源代码保存在哪里以及如何将其构建到您的应用程序中?
- teradata - 如果 Col2 = (1,2,3),则消除 ID
- java - 在使用 Java 时尝试在 Libgdx 中创建多个屏幕时,出现 com.mygdx.game.MyGdxGame cannot be cast to com.badlogic.gdx.Game 错误
- java - Java 应用程序中的 Maven 冲突与 google-cloud-core-grpc 依赖项