I have a data frame and I want to paste elements in name1, name2 and name3 which do not contain NA.
c <- data.frame(name1 = letters[1:3],
name2 = c('A', NA, 'C'),
name3 = c('pig', 'cow', NA)
)
The result should like this:
c %>% mutate(new_name = c('a&A&pig', 'b&cow', 'c&C'))
When I use paste0() it binds all the elements including NA. I do not want this.
c %>% mutate(new_name = paste0(name1,'&', name2, '&', name3))
Then I tried another two method. One is split the data frame into list with group_split(), the other is nest the data frame by index. And then use map() and select() to select the column that do not contain NA after the two methods but all failed.
c %>%
mutate(index = row_number()) %>%
group_split(index) %>%
map(select(~where(~!any(is.na(.)))))
c %>%
mutate(index = row_number()) %>%
nest(data = name1:name3) %>%
mutate(without_NA_data = map(data, select(~where(~!any(is.na(.))))))
Is there any way I can get what I want? Any help will be highly appreciated!