Removing all the columns of the data frame that have same values across all the rows

丶灬走出姿态 提交于 2019-12-11 04:05:42

问题


I have a data frame like this :

1 NA 0.2 NA 1 2
2 NA 0.5 NA 1 6 
3 NA 0.7 NA 1 4 
4 NA 0.3 NA 1 4

I want to remove the columns that have same values across all the rows.i.e my data frame should look like this:

1 0.2 2
2 0.5 6 
3 0.7 4 
4 0.3 4

Is there an easiest way to do this?


回答1:


dataf[sapply(dataf, function(x) length(unique(x))>1)]


来源:https://stackoverflow.com/questions/8776092/removing-all-the-columns-of-the-data-frame-that-have-same-values-across-all-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!