0

Referring to another post# Filtering out multiple columns in R ; there it was asked to filter out columns with all 0's (All the values in the column are 0). Using the following code:

f0 <- function(x) any(x!=0) & is.numeric(x)
trainingdata <- lapply(trainingdata, function(data) cbind(label=data$label, 
                                colwise(identity, f0)(data)))

one can filter out columns containing 0's only. There is also a need to filter out columns containing 1's only (I mean all the values in the column are 1). I tried the following:

f0 <- function(x) all(x==1) | any(x!=0) & is.numeric(x)

OR

f0 <- function(x) all(x!=1) | any(x!=0) & is.numeric(x)

but its not working.

Community
  • 1
  • 1
Shahzad
  • 1,999
  • 6
  • 35
  • 44
  • 3
    Your second function with a logical `AND` rather than `OR` and the check `any(x!=1)` should work. However, I would write these as two separate functions and do each check independently for clarity. – Justin Mar 05 '13 at 17:07
  • @Justin. Thanks. Worked perfectly. – Shahzad Mar 05 '13 at 17:14

1 Answers1

2

Your second function with a logicalAND rather than OR and the check any(x!=1) should work.

However, I would write these as two separate functions and do each check independently for clarity.

Justin
  • 42,475
  • 9
  • 93
  • 111