将DF从宽度转换为长但警告消息:要替换的项目数量不是替换长度的倍数

发布于 2025-02-07 09:35:38 字数 2008 浏览 1 评论 0原文

我想将此数据框架从宽到长。我知道该主题有一些类似的帖子,例如,我尝试了此选项中的所有选项:

用多个值列宽至长

但我一直都有相同的错误消息:值[[[JJ]] [ri]] [ri]< - if(is.factor(xij)(xij)(xij) )as.vector(xij)else xij: 要替换的项目数量不是替换长度的倍数,

我不知道如何解决此问题,有什么帮助吗?非常感谢

df3<-structure(list(id = structure(c("SA01", "SA02", "SA03", "SA04", 
"SA05", "SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12", 
"SA13", "SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20"
), label = "Code of PrevenGo", format.spss = "A5", display_width = 12L), 
    group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Metab", 
    "SA", "SC"), class = "factor"), age_1 = structure(c(11.47, 
    9.6, 6.36, 10.72, 11.32, 11.66, 11.67, 7.29, 11.85, 10.72, 
    10, 9, 11.91, 10.8, 11.1, 10.61, 11.11, 7.6, 9.24, 10.72), format.spss = "F16.2", display_width = 15L), 
    whz_1 = structure(c(-0.43, 1.55, -0.19, 0.94, 0.87, 0.32, 
    1.97, 1.54, 1.68, 1.79, 1.25, 0.78, -0.86, 1.75, 0.28, 2.02, 
    3.14, 1.67, 1.03, 2.21), label = "Weight/Length", format.spss = "F5.2", display_width = 11L), 
    age_2 = structure(c(12.02739726, 10.07945205, 6.890410959, 
    11.28219178, 11.86575342, NA, 12.21369863, 7.805479452, NA, 
    11.31780822, 9.561643836, 9.561643836, 12.46575342, 11.35616438, 
    11.66027397, 11.14520548, 11.64383562, NA, NA, 11.19726027
    ), label = "6m Age", format.spss = "F16.2", display_width = 15L), 
    whz_2 = structure(c(-0.82, 1.62, -0.2, 1.08, 0.76, NA, 1.76, 
    1.45, NA, 1.6, 1.07, 0.79, -0.92, 1.83, 0.48, 2.21, 3.22, 
    NA, NA, 2.09), label = "6m Weight/Length", format.spss = "F5.2", display_width = 11L)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

dfstack<-reshape(df3, idvar = "id", varying = list(c(3,5),c(4,6)), v.names = c("age", "whz"), direction = "long")

I would like to turn this dataframe from wide to long. I know that there are some similar posts about the topic, for instance I have tried all the options from this one:

Reshaping wide to long with multiple values columns

but I keep having the same error message: Warning in value[[jj]][ri] <- if (is.factor(xij)) as.vector(xij) else xij :
number of items to replace is not a multiple of replacement length

I don't know how I can fix this, any help? Thank you very much

df3<-structure(list(id = structure(c("SA01", "SA02", "SA03", "SA04", 
"SA05", "SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12", 
"SA13", "SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20"
), label = "Code of PrevenGo", format.spss = "A5", display_width = 12L), 
    group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Metab", 
    "SA", "SC"), class = "factor"), age_1 = structure(c(11.47, 
    9.6, 6.36, 10.72, 11.32, 11.66, 11.67, 7.29, 11.85, 10.72, 
    10, 9, 11.91, 10.8, 11.1, 10.61, 11.11, 7.6, 9.24, 10.72), format.spss = "F16.2", display_width = 15L), 
    whz_1 = structure(c(-0.43, 1.55, -0.19, 0.94, 0.87, 0.32, 
    1.97, 1.54, 1.68, 1.79, 1.25, 0.78, -0.86, 1.75, 0.28, 2.02, 
    3.14, 1.67, 1.03, 2.21), label = "Weight/Length", format.spss = "F5.2", display_width = 11L), 
    age_2 = structure(c(12.02739726, 10.07945205, 6.890410959, 
    11.28219178, 11.86575342, NA, 12.21369863, 7.805479452, NA, 
    11.31780822, 9.561643836, 9.561643836, 12.46575342, 11.35616438, 
    11.66027397, 11.14520548, 11.64383562, NA, NA, 11.19726027
    ), label = "6m Age", format.spss = "F16.2", display_width = 15L), 
    whz_2 = structure(c(-0.82, 1.62, -0.2, 1.08, 0.76, NA, 1.76, 
    1.45, NA, 1.6, 1.07, 0.79, -0.92, 1.83, 0.48, 2.21, 3.22, 
    NA, NA, 2.09), label = "6m Weight/Length", format.spss = "F5.2", display_width = 11L)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

dfstack<-reshape(df3, idvar = "id", varying = list(c(3,5),c(4,6)), v.names = c("age", "whz"), direction = "long")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文