英文:
R Make changes across multiple columns based off value in another column
问题
df1 <- df1 %>% mutate_at(vars(currentage, duestatus), ~if_else(birthdate == '1753-01-01', NA, .))
请注意,以上R代码将使用dplyr库的mutate_at
函数将currentage
和duestatus
列中的特定值更改为NA,仅当birthdate
为'1753-01-01'时。如果birthdate
不是'1753-01-01',它将保持不变。
英文:
I have a data frame where unknown birthdates are '1753-01-01'. The birthdates are used to calculate a current age and a due status. I'd like to change specific columns to NA if the birthdate is unknown and leave all others as is if the Birthdate is not unknown.
ID <- c("001", "002")
birthdate <- c("1753-01-01", "2019-01-10")
currentage <- c("98659", "1682")
duestatus <- c("due", "due")
gender <- c("F", "F")
df1 <- data.frame(ID, birthdate, currentage, duestatus, gender)
df1
ID birthdate currentage duestatus gender
1 001 1753-01-01 98659 due F
2 002 2019-01-10 1682 due F
The desired output would be like this
ID birthdate currentage duestatus gender
1 001 NA NA NA F
2 002 2019-01-10 1682 due F
I have started playing with dplyr::mutate but just can't quite get it right. The birthdate is the only value that won't change, where as current_age and duestatus will change depending on the date the code is run so ideally the code would be based off something like: when the birthdate is '1753-01-01' then change birthdate, currentage and duestatus to NA
df1 <- df1%>% mutate(case_when(birthdate == '1753-01-01' ~ NA))
答案1
得分: 1
df1 %>%
mutate(across(birthdate:duestatus,
~if_else(birthdate == '1753-01-01', NA, .)))
Result
ID birthdate currentage duestatus gender
1 001 <NA> <NA> <NA> F
2 002 2019-01-10 1682 due F
英文:
df1 %>%
mutate(across(birthdate:duestatus,
~if_else(birthdate == '1753-01-01', NA, .)))
Result
ID birthdate currentage duestatus gender
1 001 <NA> <NA> <NA> F
2 002 2019-01-10 1682 due F
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论