简体   繁体   English

隔离具有相同值的列

[英]Isolating columns with identical values

I am trying to isolate those columns of a dataframe for which all observations have the same value (ignoring NAs). 我试图隔离所有观察结果都具有相同值的数据框的那些列(忽略NA)。 See below for a hypothetical example: 参见下面的假设示例:

ForestName <- rep("Planige", 4)
TreeNumber <- c(1:4)
Height <- c(2.3, 2, 2.1, 2.9)
Type <- c("AA", "AA", NA, "AA")
df <- data.frame(ForestName, TreeNumber, Height, Type)
df

The new dataframe should contain ForestName and Type. 新数据框应包含ForestName和Type。 The columns with unequal values (TreeNumber and Height) should be contained in another dataframe. 具有不相等值(TreeNumber和Height)的列应包含在另一个数据框中。

You can use unique and check if this reduces to a single element: 您可以使用unique并检查它是否减少为单个元素:

df[sapply(df,function(x) length(unique(x[!is.na(x)])))==1]
  ForestName Type
1    Planige   AA
2    Planige   AA
3    Planige <NA>
4    Planige   AA

Or test that all elements are equal to the first non- NA : 或测试all元素是否等于第一个非NA

df[sapply(df, function(x) all(x==na.omit(x)[1],na.rm=T))]
  ForestName Type
1    Planige   AA
2    Planige   AA
3    Planige <NA>
4    Planige   AA

Among many other ways, I'm sure: 在许多其他方式中,我确信:

df[,sapply(df,function(x) {length(unique(x[!is.na(x)])) > 1})]

   TreeNumber Height
1          1    2.3
2          2    2.0
3          3    2.1
4          4    2.9

And you can negate the sapply expression to get the other columns. 您可以取反sapply表达式以获取其他列。

使用相同的基本原理的更紧凑的方法

 Filter(function(x){length(unique(x[!is.na(x)])) <=1}, df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM