In SAS data step it is easy and intuitive to change value of selected row and column based on conditions. For example:
data want;
set have;
if col1 = "A" and col2 = "B" then do;
col3 = "C";
col4 = catx(",", Col1, Col2);
end;
run;
I want to do the same thing in R. I have the following:
want <- have %>%
mutate(
col3 = ifelse(col1 == "A" & col2 == "B", "C", col3),
col4 = ifelse(col1 == "A" & col2 == "B", paste(col1, col2, sep = ","))
)
If I have more than 1 column need to change, I have to repeat the ifelse() for each column I want to change.
Is there a easier way to do it?
Thanks
=====================================
I tried this:
for (row in 1:nrow(have)) {
if (have$col1 == "A" & have$col2 == "B") {
have$col3 = 'C'
have$col4 = paste(have$col1, have$col2, sep=",")
}
}
However, mydataset is too big to loop through.
CodePudding user response:
One approach would be to save the condition in a column and use it for different columns.
library(dplyr)
want <- have %>%
mutate(
condition = col1 == "A" & col2 == "B",
col3 = ifelse(condition, "C", col3),
col4 = ifelse(condition, paste(col1, col2, sep = ","), col4)
)
CodePudding user response:
Here is an alternative way without loop:
have <- have %>% mutate(cond = col1 == "A" & col2 == "B", id = 1:nrow(have))
met <- have %>% filter(cond == TRUE) %>%
mutate(col3 = "C", col4 = paste(cyl, hp, sep = ","))
have[met$id,] <- met