Reputation: 475
I have a data frame that looks like this:
GooseIPA HeadHunter BlindPig MosaicPromise HopHunter
9 10 8 6 10
0 6 8 4 1
0 2 3 1 0
If the colSums <=10
I want to replace all values in that column with NA.
The output would look like this:
GooseIPA HeadHunter BlindPig MosaicPromise HopHunter
NA 10 8 6 10
NA 6 8 4 1
NA 2 3 1 0
Upvotes: 0
Views: 172
Reputation: 1560
If your data frame is called df
, then you can use:
df[,colSums(df)<=10]<-NA
Upvotes: 2
Reputation: 38520
Here is a method with sapply
.
is.na(df) <- sapply(df, function(x) (sum(x) < 10))
The idea is that sapply
loops through the variables and checks whether the sum of each is less than 10, if yes, TRUE is assigned to is.na
.
df
GooseIPA HeadHunter BlindPig MosaicPromise HopHunter
1 NA 10 8 6 10
2 NA 6 8 4 1
3 NA 2 3 1 0
The same method, but probably faster with colSums
is
is.na(df) <- (colSums(df) < 10)
data
df <-
dput(df)
structure(list(GooseIPA = c(9L, 0L, 0L), HeadHunter = c(10L,
6L, 2L), BlindPig = c(8L, 8L, 3L), MosaicPromise = c(6L, 4L,
1L), HopHunter = c(10L, 1L, 0L)), .Names = c("GooseIPA", "HeadHunter",
"BlindPig", "MosaicPromise", "HopHunter"), class = "data.frame", row.names = c(NA,
-3L))
Upvotes: 1