I have a dataset where cells are occupied by strings of varied length or NA
s.
I need to produce one column (say a dataframe consisting of one column) with all strings. Essentially stack all valid string answers, filtering NA
s out in the process.
df.input <- data.frame(col1 = c("green potatoes","read avocados","white pepper","wise master"),
col2 = c("white seagull","black tank","creative pigeon","crazy socks"),
col3 = c("constant turmoil","ready fan",NA,"interesting collapse"),
col4 = c("awesome lettuce","jiggedy cabbage",NA,NA),
col5 = c("green potatoes","read avocados",NA,NA),
col6 = c("green potatoes",NA,NA,NA),
col7 = c(NA,NA,NA,NA)
)
The dataframe for output
should be like:
df.output <- data.frame(colOnlyOne = c("green potatoes","read avocados","white pepper","wise master",
"white seagull","black tank","creative pigeon","crazy socks",
"constant turmoil","ready fan","interesting collapse",
"awesome lettuce","jiggedy cabbage",
"and so on for all non-NA string values...")
)
How do I achieve that? Preferably, using tidyverse family of functions.
> data.frame(colOnlyOne = na.omit(unlist(df.input, use.names = FALSE)))
colOnlyOne
1 green potatoes
2 read avocados
3 white pepper
4 wise master
5 white seagull
6 black tank
7 creative pigeon
8 crazy socks
9 constant turmoil
10 ready fan
11 interesting collapse
12 awesome lettuce
13 jiggedy cabbage
14 green potatoes
15 read avocados
16 green potatoes