gpt4 book ai didi

r - 使用 pivot_longer 将数据转换为 long 且列名重复

转载 作者:行者123 更新时间:2023-12-04 12:23:04 25 4
gpt4 key购买 nike

我有一个 df 是无限长度的。下面的例子只有 2 个特征:“密度”和“脂质”,但其他 dfs 可能有 50 个或更多特征。每个特征都有 3 列与之关联:value.trait、unit.trait、method.trait。 Seems very similiar to this example in vignette但是当我运行下面的代码时,我不断收到错误:输入必须是向量,而不是 NULL
3行样本数据

 x <- structure(list(geno_name = c("MB mixed", "MB mixed", "MB mixed"
), study_location = c("lab", "lab", "lab"), author = c("test",
"test", "test"), value.lipids = c(NA, 2.361463603, 1.461189384
), unit.lipids = c(NA, "g cm^-2", "g cm^-2"), method.lipids = c(NA,
"airbrush", "airbrush"), value.density = c(1.125257337, 0.816034359,
0.49559013), unit.density = c("g cm^-3", "g cm^-3", "g cm^-3"
), method.density = c("3D scanning", "3D scanning", "3D scanning"
)), row.names = c(NA, 3L), class = "data.frame")
当前枢轴代码:
x %>%
select(!c(study_location, author)) %>%
pivot_longer(cols = !geno_name,
names_to = c(".value", "trait"),
names_sep = ".",
values_drop_na = TRUE)
错误代码:

Error: Input must be a vector, not NULL. Run rlang::last_error() tosee where the error occurred. In addition: Warning messages: 1: Ingsub(paste0("^", names_prefix), "", names(cols)) : argument'pattern' has length > 1 and only the first element will be used 2:Expected 2 pieces. Additional pieces discarded in 6 rows [1, 2, 3, 4,5, 6].

最佳答案

我们也可以做

tidyr::pivot_longer(x, 
cols = c(lipids, density),
names_to = c('.value', 'trait'),
names_sep = '[.]',
values_drop_na = TRUE)

关于r - 使用 pivot_longer 将数据转换为 long 且列名重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66128322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com