1

I am attempting to replace specific NA values with 0 in my data table. I do not want all NAs replaces, only those under certain conditions. For example, "replace NA with Zeros when the row is Cole_1 and the Column includes the designation 'Fall1'". I have a huge data set, so I need as little manual designating as possible, numbering each column is not an option. Basically, I want to be able to target the cells like playing battleship.

I have tried:

whentest <- count_order_site %>% 
  when(select(contains("Fall1")) & 
  count_order_site[count_order_site$Point_Name == "Cole_1", ], 
  count_order_site[is.na(count_order_site)] <- 0 )  

but get an error "contains() must be used within a selecting function." I'm not even sure if this is the right path to get what I want.

The basic layout idea (Sorry it's stacked weird, I can't figure out how to make them next to each other):

Point Name ACWO_Fall1
Cole_1 NA
Cole_2 3
ACWO_FAll2 HOSP_FAll1
3 NA
NA 5

After the functions the data would look like:

Point Name ACWO_Fall1
Cole_1 0
Cole_2 3
ACWO_FAll2 HOSP_FAll1
3 0
NA 5
kjetil b halvorsen
  • 1,206
  • 2
  • 18
  • 28
Rach B.
  • 13
  • 3
  • Also, it helps to use numbers rather than "huge", which tends to just mean bigger than I'm used to. E.g. do you mean millions of rows, trillions? – Sam Mason Feb 18 '23 at 23:35
  • Yes it is R, thank you. The real data has 2,876 columns, so too big to make me want to go through manually, plus my PI would really rather a condensed code to do it. The three components do what I want individually, but I don't know how to string them together. – Rach B. Feb 19 '23 at 00:43

1 Answers1

0

If I understand correctly, you can use mutate across to include columns that contain certain character values, such as "Fall1". Then, with the replace function, replace those values that are missing using is.na and where the point_name has a specific value, such as "Cole_1".

The example below has a couple extra columns to demonstrate if the logic is correct.

library(tidyverse)

df %>%
  mutate(across(contains("Fall1"), ~replace(., is.na(.) & point_name == "Cole_1", 0)))

Output

  point_name ACWO_Fall1 ACWO_Fall2 HOSP_Fall1 Other1 Other_Fall1
1     Cole_1          0          3          0     NA           6
2     Cole_2          3         NA          5     NA          NA
Ben
  • 28,684
  • 5
  • 23
  • 45