Let’s take the mpg file
DATA1<-mpg
and add an ID as rownumber
DATA2<-dplyr::mutate(DATA1, ID = row_number())
I want to update DATA2 based on the ID for some columns only
IF ID==1 then manufacturer=”Honda”, cyl=6, year=2005,
IF ID==2 then manufacturer=”Toyota”, class=”sedan”
etc.
I have tried the following but it is wordy and does not suit large databases if the patch goes over multiple lines
DATA3 <- DATA2 %>% mutate(manufacturer = ifelse(ID == 1, “Honda”, manufacturer)) %>% mutate(cyl = ifelse(ID == 1, 6, cyl)) %>% mutate(year = ifelse(ID == 1, 2005, year))
DATA3 <- DATA2 %>% mutate(manufacturer = ifelse(ID == 2, “Toyota”, manufacturer)) %>% mutate(class = ifelse(ID == 2, “sedan”, class))