In my data frame I want to replace all values in certain columns to NA.
Test2
ID Sex Location Obs1 Obs4 Obs5
1 291978 FEMALE 2 16.5 4836 0.563636364
2 292429 FEMALE 2 20.2 5428 0.584158416
3 292466 FEMALE 2 19.2 48 0.005208333
4 293656 FEMALE 2 15.8 2904 0.417721519
5 291993 FEMALE 2 18.1 6194 0.900552486
6 293263 FEMALE 2 17.9 616 0.078212291
7 290200 FEMALE 2 16.7 792 0.107784431
8 292511 FEMALE 2 18.3 4992 0.568306011
9 291510 FEMALE 2 18.6 350 0.037634409
10 293711 FEMALE 2 18.2 264 0.032967033
11 295234 FEMALE 2 16.5 216 0.036363636
12 293839 FEMALE 2 15.0 4114 0.806666667
13 291057 FEMALE 2 16.7 56 0.005988024
14 295094 FEMALE 2 16.5 3154 0.503030303
15 295562 FEMALE 2 14.7 966 0.142857143
16 292381 FEMALE 2 17.4 1980 0.258620690
17 289765 FEMALE 2 17.8 3492 0.544943820
18 293871 FEMALE 2 18.2 3760 0.516483516
19 291076 FEMALE 2 16.8 88 0.011904762
20 293851 FEMALE 2 16.1 2242 0.366459627
Firstly, I want to specify for which columns the values should be replaced to NA. This can be only one columns, or multiple. That's why I prefer to put it into a vector.
> Obs <- c('Obs1')
Then, I've tried to replace all values in column 'Obs1' to NA, using:
> deselect <- Test2 %>% mutate(across(paste(Obs), gsub(".*",NA,paste(Obs))))
However, it gives me this error:
Error: Problem with `mutate()` input `..1`.
x Problem with `across()` input `.fns`.
i Input `.fns` must be NULL, a function, a formula, or a list of functions/formulas.
i Input `..1` is `across(paste(Obs), gsub(".*", NA, paste(Obs)))`.
Run `rlang::last_error()` to see where the error occurred.
Anyone an idea how to use gsub within across, within mutate? Or should I use another method?
Many thanks!
Or use mutate_at
:
> Obs = c("Obs1", "Obs4")
> df %>% mutate_at(Obs, function(x) x = NA)
ID Sex Location Obs1 Obs4 Obs5
1 291978 FEMALE 2 NA NA 0.563636364
2 292429 FEMALE 2 NA NA 0.584158416
3 292466 FEMALE 2 NA NA 0.005208333
4 293656 FEMALE 2 NA NA 0.417721519
5 291993 FEMALE 2 NA NA 0.900552486
6 293263 FEMALE 2 NA NA 0.078212291
7 290200 FEMALE 2 NA NA 0.107784431
8 292511 FEMALE 2 NA NA 0.568306011
9 291510 FEMALE 2 NA NA 0.037634409
10 293711 FEMALE 2 NA NA 0.032967033
11 295234 FEMALE 2 NA NA 0.036363636
12 293839 FEMALE 2 NA NA 0.806666667
13 291057 FEMALE 2 NA NA 0.005988024
14 295094 FEMALE 2 NA NA 0.503030303
15 295562 FEMALE 2 NA NA 0.142857143
16 292381 FEMALE 2 NA NA 0.258620690
17 289765 FEMALE 2 NA NA 0.544943820
18 293871 FEMALE 2 NA NA 0.516483516
19 291076 FEMALE 2 NA NA 0.011904762
20 293851 FEMALE 2 NA NA 0.366459627