I am trying to apply different functions to different rows based on the value of a string in an adjacent column. My dataframe looks like this:
type size A
if you want you can nest the ifelses:
ifelse
df$size2 <- ifelse(df$type == "A", 3*df$size, ifelse(df$type == "B", 1*df$size, ifelse(df$type == "C", 2*df$size, NA))) # > df # type size size2 #1 A 1 3 #2 B 3 3 #3 A 4 12 #4 C 2 4 #5 C 5 10 #6 A 4 12 #7 B 32 32 #8 C 3 6