Skip to content

Instantly share code, notes, and snippets.

@aammd
Created December 7, 2016 14:45
Show Gist options
  • Save aammd/74e32f27c62058ba8df4b4b74f01df51 to your computer and use it in GitHub Desktop.
Save aammd/74e32f27c62058ba8df4b4b74f01df51 to your computer and use it in GitHub Desktop.
library(dplyr)
library(tidyr)
words_some_missing <- data_frame(one_word = c(NA, "M", NA),
second_word = c("F", NA, NA))
words_some_missing %>%
unite(sex, one_word, second_word)
## that sticks the NAs together, which I think is something you dont want? In
## that case, just replace the NAs with and empty string before combining them.
## Then, combine them with another empty string as replacement:
words_some_missing %>%
replace_na(list(one_word = "", second_word = "")) %>%
unite(sex, one_word, second_word, sep = "")
# I'm assuming here that this is what you want -- you have the sex spread across
# several columns and you want to bring it into a single column?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment