我匹配了一个因子对,我想将其拆分为两个单独的列。更具体地说,我有六个人 ( individual_code
) 被测量 ( mean_mass
) 两次,因此每个人在individual_code
列中表示两次。我想生成两个单独的质量列(每个测量一个)。
这是我的数据的一个子集:
therm_sen_data %>%
filter(storage_temp == '7') %>%
subset(select = c("individual_code", "mean_mass")) %>%
head(12) %>%
dput
structure(list(individual_code = structure(c(3L, 8L, 5L, 4L,
11L, 6L, 3L, 8L, 5L, 4L, 11L, 6L), .Label = c("852", "858", "860",
"876", "879", "881", "883", "893", "908", "927", "940", "945"
), class = "factor"), mean_mass = c(2.07505, 1.3784, 1.19775,
2.1316, 1.29995, 1.60015, 2.065, 1.36275, 1.1702, 2.1384, 1.3014,
1.6056)), row.names = c(NA, 12L), class = "data.frame")
我以前用过tidyr::spread
类似的问题,但spread(individual_code, mean_mass)
由于(我假设)中的重复项而产生错误individual_code
:
Error: Each row of output must be identified by a unique combination of keys.
Keys are shared for 12 rows:
* 1, 7
* 4, 10
* 3, 9
* 6, 12
* 2, 8
* 5, 11
有没有spread
我遗漏的方面,或者除了这个功能之外我还需要什么来解决这个问题?
从 的文档中spread
,它说对 spread() 的开发已完成,对于新代码,我们建议切换到 pivot_wider(),它更易于使用,功能更多,并且仍在积极开发中。
library(tidyverse)
df %>%
group_by(individual_code) %>%
mutate(id = row_number()) %>%
pivot_wider(names_from = id, values_from = mean_mass) %>%
ungroup
# # A tibble: 6 x 3
# individual_code `1` `2`
# <fct> <dbl> <dbl>
# 1 860 2.08 2.06
# 2 893 1.38 1.36
# 3 879 1.20 1.17
# 4 876 2.13 2.14
# 5 940 1.30 1.30
# 6 881 1.60 1.61
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句