前 3 列中有重复项,我想在pivot_wider
转置后保留这些重复项,但不要在列表格式中保留。
该怎么做?
有重复的初始数据:
dat0 <-
structure(list(id = c("P1", "P1", "P1", "P1", "P1", "P1", "P1",
"P1", "P1", "P1", "P2", "P2", "P2", "P2", "P2", "P2"), analyte = c("A",
"A", "B", "B", "B", "B", "C", "C", "D", "D", "B", "B", "B", "B",
"D", "D"), analyzer = c("X", "Y", "X", "Y", "X", "Y", "X", "Y",
"X", "Y", "X", "Y", "X", "Y", "X", "Y"), result = c(0.7, 0.9,
1.26, 1.23, 1.24, 1.22, 5.7, 5.3, 4.1, 4.2, 1.22, 1.23, 1.21,
1.22, 4.4, 4.5)), row.names = c(NA, -16L), class = c("tbl_df",
"tbl", "data.frame"))
pivot_wider运行后生成以下消息:
dat1 <- dat0 %>%
pivot_wider(names_from = analyzer, values_from = result)
Values from `result` are not uniquely identified; output will contain list-cols.
• Use `values_fn = list` to suppress this warning.
• Use `values_fn = {summary_fun}` to summarise duplicates.
• Use the following dplyr code to identify duplicates.
{data} |>
dplyr::summarise(n = dplyr::n(), .by = c(id, analyte, analyzer)) |>
dplyr::filter(n > 1L)
期望输出重复项:
感谢帮助
您可以
row_number()
在分组数据中使用来区分重复项,例如,由此得出
另一个选择是利用
unnest
你已经取得的成果这也给出了所需的输出