我有一个 excel 文件,标题为:trait、genotype_id、BLUE、BLUE_SE、BLUP、BLUP_SE 和 heritability。
除特征列外,所有列都包含数字。此列具有 Fe、Zn 和 S 特征。所以我在下面有 3 组数据。我希望将这些总结为基因型,所以我有类似的东西
genotype_id, BLUE_Fe, BLUE_Zn, BLUE_S, BLUE_SE_Fe 等
我如何在 R 中转换此文件,以便特征不仅是一列,而且我在不同的列中分别具有 Fe、Zn 和 S 的特征,以便之后我可以在 R 中创建相关矩阵?
我试过了
data_wide <- spread(allBLUEs, trait, BLUE)
但这显然只会将蓝色移动到相同的行中。我试过了
data_wide <- spread(allBLUEs, key = trait, value = c(BLUE, BLUE_SE, BLUP, BLUP_SE, heritability), sep = "_")
但是value term好像只能看一列?
我的资料
df=tribble(~trait,~genotype_id,~BLUE,~BLUE_SE,~ BLUP,~BLUP_SE,~ heritability,
"Fe", 3, 47.2, 2.13, 43.0, 1.76, 0.685,
"Fe", 386, 42.5, 2.13, 39.8, 1.76, 0.685,
"Zn", 3, 24.4, 1.74, 23.6, 1.18, 0.456,
"S", 386, 1253, 51.3, 1269, 38.0, 0.545)
我想你可以用
pivot_wider
形式tidyr
来做。
library(tibble)
library(tidyr)
df=tribble(~trait,~genotype_id,~BLUE,~BLUE_SE,~ BLUP,~BLUP_SE,~ heritability,
"Fe", 3, 47.2, 2.13, 43.0, 1.76, 0.685,
"Fe", 386, 42.5, 2.13, 39.8, 1.76, 0.685,
"Zn", 3, 24.4, 1.74, 23.6, 1.18, 0.456,
"S", 386, 1253, 51.3, 1269, 38.0, 0.545)
df %>%
pivot_wider(
names_from = trait,
values_from = BLUE:heritability
)
#> # A tibble: 2 × 16
#> genoty…¹ BLUE_Fe BLUE_Zn BLUE_S BLUE_…² BLUE_…³ BLUE_…⁴ BLUP_Fe BLUP_Zn BLUP_S
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 3 47.2 24.4 NA 2.13 1.74 NA 43 23.6 NA
#> 2 386 42.5 NA 1253 2.13 NA 51.3 39.8 NA 1269
#> # … with 6 more variables: BLUP_SE_Fe <dbl>, BLUP_SE_Zn <dbl>, BLUP_SE_S <dbl>,
#> # heritability_Fe <dbl>, heritability_Zn <dbl>, heritability_S <dbl>, and
#> # abbreviated variable names ¹genotype_id, ²BLUE_SE_Fe, ³BLUE_SE_Zn,
#> # ⁴BLUE_SE_S
创建于 2023-04-08 与 reprex v2.0.2