在长和短data.frame格式之间转换

时间:2018-07-02 01:50:07

标签: r dataframe dplyr tidyr

简单的(?)tidyr问题:

我有一个data.frame,其中有多个长格式的分组列和值列。我想将其转换为短(宽)格式,其中key将是分组列之一,而生成的data.frame将有一个列,用于所有其他分组列的每个组合以及每个值列。

这是我的长格式data.frame

set.seed(1)
library(dplyr)
df <- data.frame(treatment = rep(c(rep("T1",3),rep("T2",3)),2),
                 species = c(rep("S1",6),rep("S2",6)),
                 group = rep(LETTERS[1:3],4),
                 n = as.integer(runif(12,10,20))) %>%
  dplyr::group_by(treatment,species) %>% dplyr::mutate(freq = n/sum(n))

这就是我想要的宽幅data.frame为:

res.df <- data.frame(group = LETTERS[1:3],
                     T1.S1.n = dplyr::filter(df,treatment == "T1",species == "S1")$n,
                     T1.S1.freq = dplyr::filter(df,treatment == "T1",species == "S1")$freq,
                     T2.S1.n = dplyr::filter(df,treatment == "T2",species == "S1")$n,
                     T2.S1.freq = dplyr::filter(df,treatment == "T2",species == "S1")$freq,
                     T1.S2.n = dplyr::filter(df,treatment == "T1",species == "S2")$n,
                     T1.S2.freq = dplyr::filter(df,treatment == "T1",species == "S2")$freq,
                     T2.S2.n = dplyr::filter(df,treatment == "T2",species == "S2")$n,
                     T2.S2.freq = dplyr::filter(df,treatment == "T2",species == "S2")$freq)

1 个答案:

答案 0 :(得分:1)

我们可以使用gather转换为'long'格式,然后将unite列合并,然后将spread转换为'wide'

library(tidyverse)
gather(df, key, val, n:freq) %>% 
     unite(trtsp, treatment, species, key, sep = ".") %>%
     spread(trtsp, val)