How can I replace values of multiple columns based on one or two conditions below
# replace values of multiple rows based on one and two column
data <- data.frame(x1 = 1:5,
date = c("2020-01-25", NA,"2021-03-15",NA,"2021-05-11"),
x2 = c(" ", "Town", "Village", "Farm"," "),
x3 = c(200L, NA,50L,NA,100L),
x4 = factor(c("B", NA, "A", "F", NA)),
stringsAsFactors = FALSE)
# I would like to do the following manual statements seperately
# If x1=1, then date = 2022-02-25, x2= A, and x4= x
# If x2= Farm and x4=F, then date= 2020-01-15, x3= 70
This code does both sets of changes, which may not be what you want but you can break it up into two mutate function if necessary.
data <- mutate(data,
date = ifelse(x1 == 1, "2022-02-25", date),
x2 = ifelse(x1 == 1, "A", x2),
x4 = ifelse(x1 == 1, "x", x4),
date = ifelse(x2 == "Farm" & x4 == "F", "2022-01-15", date),
x3 = ifelse(x3 == "Farm" & x4 == "F", 70, x3))
Thank you very much for your quick response and help. When I applied the first part of the code to my real data, it changed the entire rows for selected columns. My real data looks like below and I applied following part of your code . It works in this example, but does not give me the desired result for my real data. Please let me know if there is an equivalent function to use. Thank you again.
data2 <- data.frame(x1 = 1:5,
date = c(NA,"2020-01-25", "2021-03-15",NA,"2021-05-11"),
x2 = c("Town", " ", "Village", "Farm"," "),
x3 = c(NA, NA,50L,NA,100L),
x4 = factor(c(NA, NA, "A", "F", NA))
)
data2 <- mutate(data2,
date = ifelse(x2 == "Town", "2022-02-25", date),
x1 = ifelse(x2 == "Town", 1, x1),
x3 = ifelse(x2 == "Town", 100, x3),
x4 = ifelse(x2 == "Town", "X", x4))
In your latest example, column x4, which is a factor, gets corrupted. The original A and F become "1" and "2". Those are character versions of the original factor values. Is that the same sort of thing you are seeing with your full data? Unfortunately, I don't know how to fix it.
data2 <- data.frame(x1 = 1:5,
date = c(NA,"2020-01-25", "2021-03-15",NA,"2021-05-11"),
x2 = c("Town", " ", "Village", "Farm"," "),
x3 = c(NA, NA,50L,NA,100L),
x4 = factor(c(NA, NA, "A", "F", NA))
)
data2
#> x1 date x2 x3 x4
#> 1 1 <NA> Town NA <NA>
#> 2 2 2020-01-25 NA <NA>
#> 3 3 2021-03-15 Village 50 A
#> 4 4 <NA> Farm NA F
#> 5 5 2021-05-11 100 <NA>
library(dplyr)
data2 <- mutate(data2,
date = ifelse(x2 == "Town", "2022-02-25", date),
x1 = ifelse(x2 == "Town", 1, x1),
x3 = ifelse(x2 == "Town", 100, x3),
x4 = ifelse(x2 == "Town", "X", x4))
data2
#> x1 date x2 x3 x4
#> 1 1 2022-02-25 Town 100 X
#> 2 2 2020-01-25 NA <NA>
#> 3 3 2021-03-15 Village 50 1
#> 4 4 <NA> Farm NA 2
#> 5 5 2021-05-11 100 <NA>
Created on 2022-06-13 by the reprex package (v2.0.1)
I use forcats::expand to add 'X' as a possible level for x4, also switch to dplyr::if_else for easier to work with factor behaviour.
library(tidyverse)
data2 <- data.frame(x1 = 1:5,
date = c(NA,"2020-01-25", "2021-03-15",NA,"2021-05-11"),
x2 = c("Town", " ", "Village", "Farm"," "),
x3 = c(NA, NA,50L,NA,100L),
x4 = factor(c(NA, NA, "A", "F", NA))
)
data2$x4 <- fct_expand(data2$x4,"X")
x_fac <- factor("X",levels=levels(data2$x4))
(data3 <- mutate(data2,
date = ifelse(x2 == "Town", "2022-02-25", date),
x1 = ifelse(x2 == "Town", 1, x1),
x3 = ifelse(x2 == "Town", 100, x3),
x4 = if_else(x2 == "Town", x_fac, x4)))
data3
Thank you both for your help. I did not work in my real data, and I had do it manually.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.