Genevieve
Genevieve

Reputation: 11

Need help replacing values with NA when another condition is met in R (i.e. when another variable is a specific value)

I'm trying to delete some repeating information in my data set and replace it with NA. Here's an example of the data:

DataTable1

ID          Day         x         y  
1           1           1         3  
1           2           1         3  
2           1           2         5  
2           2           2         5  
3           1           3         4  
3           2           3         4  
4           1           4         6  
4           2           4         6  

I'm trying to replace "x" and "y" values with "NA" when Day=1. This is what I want:

ID          Day         x         y  
1           1           NA        NA  
1           2           1         3  
2           1           NA        NA  
2           2           2         5  
3           1           NA        NA  
3           2           3         4  
4           1           NA        NA 
4           2           4         6  

I'm not really sure where to start or how to go about this. I tried using the replace_with_na_if function from the naniar library. Otherwise, I am unsure what to try.

replace_with_na_if(data.frame=DataTable1$x,
                   condition=DataTable1$Day== 2)

I received an error message that reads:

Error in replace_with_na_if(data.frame = DataTable1$x, condition = DataTable1$Day == : unused argument (data.frame = DataTable1$x)

Upvotes: 1

Views: 399

Answers (3)

FG7
FG7

Reputation: 469

Here's a data.table solution. Since you may be new to R, you need to install the data.table package first. If you have a large data set, data.table may work faster than using data frame. Also, I find the syntax to be easy to read and understand.

#Create the data frame:
df <- structure(list(ID = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), Day = c(1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L), x = c(1L, 1L, 2L, 2L, 3L, 3L, 4L,  4L), y = c(3L, 3L, 5L, 5L,
4L, 4L, 6L, 6L)), class = "data.frame", row.names = c(NA, -8L))

library(data.table)
dt <- setDT(df) # convert the data frame to a data.table
dt[Day == 1, c("x","y") := NA] # where Day equals 1, make the columns x and y equal NA

Good luck and welcome to stackoverflow!

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389325

Using dplyr, we can use mutate_at and replace like

library(dplyr)
df %>%  mutate_at(vars(x, y), ~replace(., Day == 1, NA))


#  ID Day  x  y
#1  1   1 NA NA
#2  1   2  1  3
#3  2   1 NA NA
#4  2   2  2  5
#5  3   1 NA NA
#6  3   2  3  4
#7  4   1 NA NA
#8  4   2  4  6

data

df <- structure(list(ID = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), Day = c(1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L), x = c(1L, 1L, 2L, 2L, 3L, 3L, 4L,  4L), y = c(3L, 3L, 5L, 5L,
4L, 4L, 6L, 6L)), class = "data.frame", row.names = c(NA, -8L))

Upvotes: 0

akrun
akrun

Reputation: 887961

An option in base R would be to create a logical vector based on the elements of 'Day'. Use that index to subset the 'x', 'y' columns and assign them to NA

i1 <- df1$Day == 1
df1[i1, c('x', 'y')] <- NA

Upvotes: 2

Related Questions