传播数据框并重新标记列名

Spread a dataframe and relabel column names

可能是 tidyrreshape2 问题。

我有这种 data.frame:

df <- data.frame(group = c("A","B","C","D"),
                 id1 = c("AV14D","BV29",NA,NA),
                 id2 = c(NA,"BD1",NA,NA),
                 id3 = c("AJ31","BJ1",NA,NA),
                 n = c(2,4,NA,NA),
                 stringsAsFactors = F)

其中一行包含 3 个 ID 和每个组的计数。

我想将其转换为单行 data.frame,包含 #groups x 4 (id1,id2,id3,n) 列,其中每个组有 4 列:<group>_id1<group>_id2<group>_id3<group>_n 以及相应的值。

因此结果 data.frame 将是:

data.frame(A_id1 = "AV14D",A_id2 = NA,A_id3 = "AJ31",A_n = 2,
           B_id1 = "BV29",B_id2 = "BD1",B_id3 = "BJ1",B_n = 4,
           C_id1 = NA,C_id2 = NA,C_id3 = NA,C_n = NA,
           D_id1 = NA,D_id2 = NA,D_id3 = NA,D_n = NA,
           stringsAsFactors = F)

我们可以在创建行索引列后使用pivot_wider

library(dplyr)
library(tidyr)
library(stringr)
out1 <- df %>%
  mutate(rn = 1) %>% 
  pivot_wider(names_from = group, values_from = id1:n,
        names_glue = "{group}_{.value}") %>% 
  select(order(str_remove(names(.), "_.*")), -rn) %>%
  type.convert(as.is = TRUE)

-检查 OP 的输出

> all.equal(out, out1, check.attributes = FALSE)
[1] TRUE