如何将R中的信息从行转换为列

问题描述

我有一个名为“df”的日期集:

df <-  structure(list(outcome = c("cg00000029","cg00000029","cg00000108","cg00000108"),pval = c("0.63","0.91","0.01","0.09","0.55","0.23")),.Names = c("outcome","pval"),class = "data.frame",row.names = c(NA,-6L))

enter image description here

如何将其转换为名为“df1”的数据集?

df1 <-  structure(list(outcome = c("cg00000029",pval_1 = c("0.63","0.91"),pval_2 = c("0.01","0.09"),pval_3 = c("0.55","pval_1","pval_2","pval_3"),-2L))

enter image description here

谢谢!

解决方法

使用 data.tabledcast 选项

> dcast(setDT(df),outcome ~ paste0("pval_",rowid(outcome)))
Using 'pval' as value column. Use 'value.var' to override
      outcome pval_1 pval_2 pval_3
1: cg00000029   0.63   0.91   0.01
2: cg00000108   0.09   0.55   0.23
,

这是一个 tidyverse 方法:

library(dplyr)
library(tidyr)

df %>%
  group_by(outcome) %>%
  mutate(id = row_number()) %>%
  pivot_wider(names_from = id,values_from = pval,names_glue = "{.value}_{id}")

# A tibble: 2 x 4
# Groups:   outcome [2]
  outcome    pval_1 pval_2 pval_3
  <chr>      <chr>  <chr>  <chr> 
1 cg00000029 0.63   0.91   0.01  
2 cg00000108 0.09   0.55   0.23