Reputation: 9752
I have the following data frame
school<-c("NYU", "BYU", "USC")
state<-c("NY","UT","CA")
measure<-c("MSAT","MSAT","GPA")
score<-c(500, 490, 2.9)
score2<-(c(200, 280, 4.3))
df<-data.frame(school,state, measure,score,score2, stringsAsFactors=FALSE)
> df
school state measure score score2
1 NYU NY MSAT 500.0 200.0
2 BYU UT MSAT 490.0 280.0
3 USC CA GPA 2.9 4.3
And I would like to set all the values for certain columns to NA without any condition. Just set them to NA. i.e.
> df
school state measure score score2
1 NYU NA MSAT NA NA
2 BYU NA MSAT NA NA
3 USC NA GPA NA NA
I have tried:
df <- mutate_at(vars(-school,-measure),na_if(.,!is.na(.)))
Where I was expecting na_if(.,!is.na(.))
to convert any value that wasn't already NA to NA. But as you can see I'm not correctly feeding the columns into the is.na()
function.
Error in length_x %in% c(1L, n) : object '.' not found
How would I go about achieving this. I have many more columns I would like the perform this on than columns I want to preserve.
Upvotes: 0
Views: 256
Reputation: 5766
Since you're asking specifically for mutate
, here are some things to consider:
In your line df <- mutate_at(vars(-school,-measure),na_if(.,!is.na(.)))
, it fails because it expects df
as the first argument - or piped in. The correct usage would be
df <- df %>% mutate_at(vars(-school,-measure),na_if(.,!is.na(.)))
But that doesn't solve it, because
na_if
and is.na
does? Just a quick ?na_if
?? Because it doesn't replace values when the second argument is true, but replaces values equal to the second argument with NA
. So, that just plainly doesn't work as expected.And finally,
Which leads to the following solution:
just.na <- function(x) rep(NA, length(x))
df %>% mutate_at(vars(-school, -measure), just.na)
Or, an anonymous function:
df %>% mutate_at(vars(-school, -measure), ~rep(NA, length(.)))
Or, it turns out you can do this:
df %>% mutate_at(vars(-school, -measure), ~NA)
(I am equally surprised!)
Upvotes: 2