r - How to modify a single column with joins using dplyr -


i'm trying add new column data frame, based on levels of 1 (or few) factors. start data frame 2 factors , single variable

library(dplyr) test <- data_frame(one = letters[1:5], 2 = letters[1:5], 3 = 6:10) 

and want add new column, four, has values levels of one , two. convenience, keep these new values in own little tables:

new_fourth_a <- data_frame(one = "b", 4 = 47) new_fourth_b <- data_frame(two = c("c","e"), 4 = 42) 

the correct answer

    1   2 3  4   (chr) (chr) (int) (dbl) 1             6    na 2     b     b     7    47 3     c     c     8    42 4     d     d     9    na 5     e     e    10    42 

and best way think of accomplish via left_join():

test %>%    left_join(new_fourth_a, = "one") %>%   left_join(new_fourth_b, = "two") 

but ends duplicating four column. thing: allow easy checking see if there joins introduce more 1 value new column (ie check there 1 non-na value across each row in columns start four. ). still, think there must easier way?

here solution uses join

library(dplyr) test <- data_frame(one = letters[1:5], 2 = letters[1:5], 3 = 6:10) new_fourth_a <- data_frame(one = "b", extra_a = 47) new_fourth_b <- data_frame(two = c("c","e"), extra_b = 42) test %>%    left_join(new_fourth_a, = "one") %>%   left_join(new_fourth_b, = "two") %>%   mutate(four = pmax(extra_a, extra_b, na.rm = true)) %>%   select(-extra_a, -extra_b) 

if want handle arbitrary number have handle 1 @ time

library(dplyr) test <- data_frame(one = letters[1:5], 2 = letters[1:5], 3 = 6:10) new_fourth_a <- data_frame(one = "b", = 47) new_fourth_b <- data_frame(two = c("c","e"), = 42) test %>%    left_join(new_fourth_a, = "one") %>%   mutate(four = extra) %>%   select(-extra) %>%   left_join(new_fourth_b, = "two") %>%   mutate(four = ifelse(is.na(extra), four, extra)) %>%   select(-extra) 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -