简单的(?)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)
答案 0 :(得分:1)
我们可以使用gather
转换为'long'格式,然后将unite
列合并,然后将spread
转换为'wide'
library(tidyverse)
gather(df, key, val, n:freq) %>%
unite(trtsp, treatment, species, key, sep = ".") %>%
spread(trtsp, val)