I have to insert 0 into NA and missing values in a csv file

moviedata$screens[is.na(moviedata$budget) & moviedata$budget==""] <- 0
Warning message:
In [<-.factor(*tmp*, is.na(moviedata$budget) & moviedata$budget == :
invalid factor level, NA generated

Hi Rock,

If I understand correctly, if budget is NA, you want to change the value of screens to 0. Have you considered using case_when inside of mutate?:

library(tidyverse)

moviedata <- moviedata %>% 
  mutate(screens = case_when(
    is.na(budget) ~ 0,
    TRUE ~ screens
  ))

Created on 2018-10-01 by the reprex package (v0.2.0).

If this isn't what you're looking for, can you please share a reprex (reproducible example)? This will ensure we're all looking at the same data and code. A guide for creating a reprex can be found here.

The problem likely comes from when you read in moviedata. You seem to expect the "screens" column to be a numeric vector, but it's a factor. That's the default class for strings read by base R's reading functions (read.table and the like). So something's messing with how it's being read.

Without seeing the data file and knowing everything you're trying to do, I can't recommend a "best" way. But some solutions are to use the colClasses argument in the reader function or coercing the "screens" column to numeric after reading it in.

Also, if you want to replace values that are missing or blank, you need to use that operator:

moviedata$screens[is.na(moviedata$budget) | moviedata$budget==""] <- 0
1 Like