I have a dataset df1 with columns from S_2018_p to S_2021_p where I want to replace the values >= 10 with 0. I would expect a dataset like df2.
library(data.table)
df1 = data.table(
ID = c("a1", "a2", "a3", "a4", "a5", "a6", "a7"),
"string1" = c("x2", "g3", "n2", "m3", "2w", "ps2", "kg2"),
"S_2018_p" = c(3,5,11,3,9,22,6),
"S_2019_p" = c(3,5,6,21,1,4,0),
"S_2020_p" = c(0,4,13,9,16,7,9),
"S_2021_p" = c(4,0,3,8,5,4,6),
"string2" = c("si", "q2", "oq", "mx", "ix", "p2", "2q"))
ID string1 S_2018_p S_2019_p S_2020_p S_2021_p string2
1: a1 x2 3 3 0 4 si
2: a2 g3 5 5 4 0 q2
3: a3 n2 11 6 13 3 oq
4: a4 m3 3 21 9 8 mx
5: a5 2w 9 1 16 5 ix
6: a6 ps2 22 4 7 4 p2
7: a7 kg2 6 0 9 6 2q
df2 = data.table(
ID = c("a1", "a2", "a3", "a4", "a5", "a6", "a7"),
"string1" = c("x2", "g3", "n2", "m3", "2w", "ps2", "kg2"),
"S_2018_p" = c(3,5,0,3,9,0,6),
"S_2019_p" = c(3,5,6,0,1,4,0),
"S_2020_p" = c(0,4,0,9,0,7,9),
"S_2021_p" = c(4,0,3,8,5,4,6),
"string2" = c("si", "q2", "oq", "mx", "ix", "p2", "2q"))
ID string1 S_2018_p S_2019_p S_2020_p S_2021_p string2
1: a1 x2 3 3 0 4 si
2: a2 g3 5 5 4 0 q2
3: a3 n2 0 6 0 3 oq
4: a4 m3 3 0 9 8 mx
5: a5 2w 9 1 0 5 ix
6: a6 ps2 0 4 7 4 p2
7: a7 kg2 6 0 9 6 2q
I tried to do it with mutete_if, but I don't get the desired output.
df1 %>%
mutate_if(is.numeric, ~1 * (. >= 10))