我目前有一个“长”数据框,看起来像这样:
# Phylum Sample Abundance
# Other A1 17
# Other A2 0
# Actinobacteria A3 123
# Actinobacteria B1 651
# Other B2 22
# Actinobacteria B3 844
# Firmicutes A1 403
# Other A2 42
# Firmicutes A3 225
# Other B1 16
# Other B2 19
# Firmicutes B3 556
我的目标是使它看起来像这样,在该数据中,数据被旋转到较宽的位置,但是对重复的值求和(例如Other / A2和Other / B2):
# Phylum A1 A2 A3 B1 B2 B3
# Actinobacteria 0 0 123 651 0 844
# Firmicutes 403 0 225 0 0 556
# Other 17 71 0 16 41 0
到目前为止,我已经尝试过
newdata <- olddata %>% pivot_wider(names_from="Sample",values_from="Abundance",values_fill=0,values_fn=sum)
但这给出了错误Error in values_fn[[value]] : object of type 'builtin' is not subsettable
。我曾尝试在本网站的其他地方查找,但大多数人建议制作唯一的行名,这对我而言不是一个选择。任何帮助将不胜感激。谢谢!
答案 0 :(得分:1)
无法重现该错误,但最好将values_fill
和values_fn
放在list
library(tidyr)
library(dplyr)
olddata %>%
pivot_wider(names_from= Sample ,values_from= Abundance,
values_fill=list(Abundance = 0),values_fn= list(Abundance = sum))
# A tibble: 3 x 7
# Phylum A1 A2 A3 B1 B2 B3
# <chr> <int> <int> <int> <int> <int> <int>
#1 Other 17 42 0 16 41 0
#2 Actinobacteria 0 0 123 651 0 844
#3 Firmicutes 403 0 225 0 0 556
olddata <- structure(list(Phylum = c("Other", "Other", "Actinobacteria",
"Actinobacteria", "Other", "Actinobacteria", "Firmicutes", "Other",
"Firmicutes", "Other", "Other", "Firmicutes"), Sample = c("A1",
"A2", "A3", "B1", "B2", "B3", "A1", "A2", "A3", "B1", "B2", "B3"
), Abundance = c(17L, 0L, 123L, 651L, 22L, 844L, 403L, 42L, 225L,
16L, 19L, 556L)), class = "data.frame", row.names = c(NA, -12L
))