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:
这是输出:
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"))
我拥有的真实数据框有更多的行和更多的列。我想将它们组合起来,以便 ID 1 的所有内容都在一行上,ID 2 的所有内容都在一行上,依此类推。我已经尝试过,但它没有让我有任何进展
qr <- qr %>%
group_by(., ID) %>%
rowwise() %>%
summarise_all(funs(first(na.omit(.))))
我收到错误:
Error in summarise_impl(.data, dots) :
Column `first name` must be length 1 (a summary value), not 0
我也尝试过 dcast 但这也没有帮助。谢谢!