brucezepplin
brucezepplin

Reputation: 9752

How do I convert all vector elements to NA (no condition)

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

Answers (3)

MrGumble
MrGumble

Reputation: 5766

Since you're asking specifically for mutate, here are some things to consider:

  1. 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

  1. Have you checked what 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,

  1. Why only change non-NA values to NA? Why not just change everything to NA?

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

dmuenzel
dmuenzel

Reputation: 101

This would do it

df[,c("state","score","score2")]<-NA

Upvotes: 4

Alice
Alice

Reputation: 191

Try this:

df[,c(2,4,5)] <- NA

Upvotes: 0

Related Questions