我有一个 R 中的数据框矩阵,希望按列之和降序排序。我的数据范围从 +1 到 -1。我有一个代码可以完美地实现这个目标:
DF<-DF[, order(colSums(-DF))]
然而,我的一些数据中存在NA值(没有任何单个列或行是全部NA,因此我不能简单地删除整个列或行)。我认为数据没有被正确排序,因为包含NA的列没有被排序,只是放在已排序的列后面。
是否有一种方法可以按列总和对数据进行排序,同时也允许对包含NA的列进行排序?
colSums()
函数应用于NA列中非NA单元格的结果来对NA列进行排序。你可以通过向order()
添加一个额外的参数来实现这一点,并在其中使用na.rm=TRUE
调用colSums()
函数来打破连接。以下是一个演示,总共有4列,其中2列有NAs,2列没有:set.seed(3L)
df <- setNames(rev(as.data.frame(replicate(4L,
sample(c(seq(-1,1,0.5),NA),
5L,rep=TRUE)))),letters[1:4])
df ## columns a and b are "NA columns", columns c and d are "non-NA columns"
## a b c d
## 1 1.0 0.5 0.5 -0.5
## 2 -1.0 0.5 -1.0 1.0
## 3 1.0 0.5 -0.5 0.0
## 4 NA 0.5 0.5 -0.5
## 5 -0.5 NA 0.5 0.5
colSums(-df) ## d should be moved before c, but can't tell yet about a and b
## a b c d
## NA NA 0.0 -0.5
colSums(-df,na.rm=TRUE) ## this can tiebreak a and b; b should be moved before a
## a b c d
## -0.5 -2.0 0.0 -0.5
df[,order(colSums(-df))] ## fails to order NA columns
## d c a b
## 1 -0.5 0.5 1.0 0.5
## 2 1.0 -1.0 -1.0 0.5
## 3 0.0 -0.5 1.0 0.5
## 4 -0.5 0.5 NA 0.5
## 5 0.5 0.5 -0.5 NA
df[,order(colSums(-df),colSums(-df,na.rm=TRUE))] ## tiebreaker orders NA columns properly
## d c b a
## 1 -0.5 0.5 0.5 1.0
## 2 1.0 -1.0 0.5 -1.0
## 3 0.0 -0.5 0.5 1.0
## 4 -0.5 0.5 0.5 NA
## 5 0.5 0.5 NA -0.5
df[,order(colSums(-df,na.rm=TRUE))]
## b a d c
## 1 0.5 1.0 -0.5 0.5
## 2 0.5 -1.0 1.0 -1.0
## 3 0.5 1.0 0.0 -0.5
## 4 0.5 NA -0.5 0.5
## 5 NA -0.5 0.5 0.5
na.rm=TRUE
等同于将NA视为零,这与您的规定相反,即将NA视为零会破坏排序。DF<-DF[, order(colSums(-DF, na.rm=T))]
order(c(1,NA,3,NA))
并且看到 NAs 确实被分配了最后的顺序。要修改它,也许可以使用?order
中提到的na.last
选项。?colSums
显示它有一个na.rm
选项,你可能想要使用它。顺便说一下,[dataframes] 不是 R 的正确标签。 - Frank