I have been searching a lot but I can't seem to find an answer for what I'm looking for. The rows were originally melted together and then I spread them and now I have a data frame that look similar to this:
Here is the dput:
structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L),
`first name` = c("Jamie", NA, NA, NA, NA, "sandra", NA, NA,
NA, NA), `last name` = c(NA, "Johns", NA, NA, NA, NA, NA,
"chan", NA, NA), q1_ans = c(NA, NA, "yes", NA, NA, NA, "yes",
NA, NA, NA), q2_ans = c(NA, NA, NA, "no", NA, NA, NA, NA,
"yes", NA), q3_ans = c(NA, NA, NA, NA, "yes", NA, NA, NA,
NA, "no")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"), spec = structure(list(cols = list(ID = structure(list(), class = c("collector_integer",
"collector")), `first name` = structure(list(), class = c("collector_character",
"collector")), `last name` = structure(list(), class = c("collector_character",
"collector")), q1_ans = structure(list(), class = c("collector_character",
"collector")), q2_ans = structure(list(), class = c("collector_character",
"collector")), q3_ans = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector"))), class = "col_spec"))
The real dataframe I have has much more rows and a few more columns. I want to combine them so that everything for ID 1 is on one line, and ID 2 in one row, and so on. I've tried this but it hasn't gotten me anywhere
qr <- qr %>%
group_by(., ID) %>%
rowwise() %>%
summarise_all(funs(first(na.omit(.))))
I get the error:
Error in summarise_impl(.data, dots) :
Column `first name` must be length 1 (a summary value), not 0
I also tried dcast but that didn't help either. Thanks!