在我的数据框中,我想将某些列中的所有值替换为NA。
Test2 IDSex Location Obs1 Obs4Obs51291978 FEMALE2 16.5 4836 0.5636363642292429 FEMALE2 20.2 5428 0.5841584163292466 FEMALE2 19.2 48 0.0052083334293656 FEMALE2 15.8 2904 0.4177215195291993 FEMALE2 18.1 6194 0.9005524866293263 FEMALE2 17.9616 0.0782122917290200 FEMALE2 16.7792 0.1077844318292511 FEMALE2 18.3 4992 0.5683060119291510 FEMALE2 18.6350 0.03763440910 293711 FEMALE2 18.2264 0.03296703311 295234 FEMALE2 16.5216 0.03636363612 293839 FEMALE2 15.0 4114 0.80666666713 291057 FEMALE2 16.7 56 0.00598802414 295094 FEMALE2 16.5 3154 0.50303030315 295562 FEMALE2 14.7966 0.14285714316 292381 FEMALE2 17.4 1980 0.25862069017 289765 FEMALE2 17.8 3492 0.54494382018 293871 FEMALE2 18.2 3760 0.51648351619 291076 FEMALE2 16.8 88 0.01190476220 293851 FEMALE2 16.1 2242 0.366459627
首先,我想指定应将哪些列的值替换为NA。只能是一列,也可以是多列。这就是为什么我更喜欢将其放入向量。
> Obs <- c('Obs1')
然后,我尝试使用以下命令将“ Obs1”列中的所有值替换为NA:
> deselect <- Test2 %>% mutate(across(paste(Obs), gsub(".*",NA,paste(Obs))))
但是,它给了我这个错误:
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.
有人知道如何在mutate内,mutate内使用gsub吗?还是应该使用其他方法?
非常感谢!
我建议一种base R
方法,您在Obs
各列中定义以替换:
#Datadf <- structure(list(ID = c(291978L, 292429L, 292466L, 293656L, 291993L, 293263L, 290200L, 292511L, 291510L, 293711L, 295234L, 293839L, 291057L, 295094L, 295562L, 292381L, 289765L, 293871L, 291076L, 293851L), Sex = c("FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE", "FEMALE"), Location = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Obs1 = c(16.5, 20.2, 19.2, 15.8, 18.1, 17.9, 16.7, 18.3, 18.6, 18.2, 16.5, 15, 16.7, 16.5, 14.7, 17.4, 17.8, 18.2, 16.8, 16.1), Obs4 = c(4836L, 5428L, 48L, 2904L, 6194L, 616L, 792L, 4992L, 350L, 264L, 216L, 4114L, 56L, 3154L, 966L, 1980L, 3492L, 3760L, 88L, 2242L), Obs5 = c(0.563636364, 0.584158416, 0.005208333, 0.417721519, 0.900552486, 0.078212291, 0.107784431, 0.568306011, 0.037634409, 0.032967033, 0.036363636, 0.806666667, 0.005988024, 0.503030303, 0.142857143, 0.25862069, 0.54494382, 0.516483516, 0.011904762, 0.366459627)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"))
接下来的代码:
#Define colsObs <- c('Obs1')#Assignindex <- which(names(df) %in% Obs) df[,index] <- gsub(".*",NA,df[,index])
输出:
IDSex Location Obs1 Obs4Obs51291978 FEMALE2 <NA> 4836 0.5636363642292429 FEMALE2 <NA> 5428 0.5841584163292466 FEMALE2 <NA> 48 0.0052083334293656 FEMALE2 <NA> 2904 0.4177215195291993 FEMALE2 <NA> 6194 0.9005524866293263 FEMALE2 <NA>616 0.0782122917290200 FEMALE2 <NA>792 0.1077844318292511 FEMALE2 <NA> 4992 0.5683060119291510 FEMALE2 <NA>350 0.03763440910 293711 FEMALE2 <NA>264 0.03296703311 295234 FEMALE2 <NA>216 0.03636363612 293839 FEMALE2 <NA> 4114 0.80666666713 291057 FEMALE2 <NA> 56 0.00598802414 295094 FEMALE2 <NA> 3154 0.50303030315 295562 FEMALE2 <NA>966 0.14285714316 292381 FEMALE2 <NA> 1980 0.25862069017 289765 FEMALE2 <NA> 3492 0.54494382018 293871 FEMALE2 <NA> 3760 0.51648351619 291076 FEMALE2 <NA> 88 0.01190476220 293851 FEMALE2 <NA> 2242 0.366459627
或使用mutate_at
:
> Obs = c("Obs1", "Obs4")> df %>% mutate_at(Obs, function(x) x = NA) IDSex Location Obs1 Obs4Obs51291978 FEMALE2 NA NA 0.5636363642292429 FEMALE2 NA NA 0.5841584163292466 FEMALE2 NA NA 0.0052083334293656 FEMALE2 NA NA 0.4177215195291993 FEMALE2 NA NA 0.9005524866293263 FEMALE2 NA NA 0.0782122917290200 FEMALE2 NA NA 0.1077844318292511 FEMALE2 NA NA 0.5683060119291510 FEMALE2 NA NA 0.03763440910 293711 FEMALE2 NA NA 0.03296703311 295234 FEMALE2 NA NA 0.03636363612 293839 FEMALE2 NA NA 0.80666666713 291057 FEMALE2 NA NA 0.00598802414 295094 FEMALE2 NA NA 0.50303030315 295562 FEMALE2 NA NA 0.14285714316 292381 FEMALE2 NA NA 0.25862069017 289765 FEMALE2 NA NA 0.54494382018 293871 FEMALE2 NA NA 0.51648351619 291076 FEMALE2 NA NA 0.01190476220 293851 FEMALE2 NA NA 0.366459627
这是您使用mutate
和的方式across
。
cols_na <- c("wt", "hp")mtcars %>% mutate(across(one_of(cols_na), ~ NA))