I have a variable PC_R
in dataframe jd_df
that describes laboratory test results. I'd like to replace some of the data in this variable (eg tf, QNS, rej) with NA. I've tried this code:
jd_df %>%
replace(PC_R,TF,NA)
and this:
jd_df %>%
replace(jd_df,PC_R==TF,NA)
and this:
jd_df %>%
replace(PC_R,"TF","NA")
and this:
jd_df %>%
replace(jd_df,PC_R%in%TF,NA)
I keep getting the error:
Error in replace(., jd_df, PC_R %in% TF, NA) : unused argument (NA)
I'm wondering if the replace command is not the way to go.
You can achieve this using case_when as demonstrated using the iris dataset below
library(dplyr)
iris <- iris %>%
mutate(Species = as.character(Species)) %>%
mutate(Species = case_when(
Species == "setosa" ~ NA_character_,
TRUE ~ Species
))
Multiple changes can be specified as such:
iris %>%
mutate(Species = as.character(Species)) %>%
mutate(Species = case_when(
Species == "setosa" | Species == "versicolor" ~ NA_character_,
TRUE ~ Species
))
Created on 2019-02-16 by the reprex package (v0.2.0).
我发现这可以将多个字符文本更改为NA:
jd_df %>% mutate(PC_R = replace(PC_R, PC_R %in% "TF"|PC_R %in% "tf"|PC_R %in% "rej",NA))
The case_when()
answer above works well! A simpler alternative is the na_if()
function, which replaces a specified string with NA
. As such:
library(dplyr)
iris %>%
mutate(Species = na_if(Species, "setosa"))
This will change all instances of setosa
to NA
in the column Species
. In your case, it could be:
jd_df %>%
mutate(PC_R = na_if(PC_R, "TF"))
Which replaces all "TF"
with NA
. You can repeat the code as needed to catch all your intended NA
values:
jd_df %>%
mutate(PC_R = na_if(PC_R, "TF"),
PC_R = na_if(PC_R, "QNS"),
PC_R = na_if(PC_R, "rej"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.