gpt4 book ai didi

r - 如何在 base R 中通过 reshape 重现 pivot_longer 的结果?

转载 作者:行者123 更新时间:2023-12-05 09:29:49 24 4
gpt4 key购买 nike

考虑 cprefm 对象:

library(conjoint)

data(chocolate)

使用pivot_longer:

library(dplyr)
library(tidyr)

cprefm %>%
pivot_longer(., 1:16, "profile", "rating") %>%
head(16)

# A tibble: 16 × 2
profile value
<chr> <int>
1 profile1 14
2 profile2 15
3 profile3 5
4 profile4 2
5 profile5 1
6 profile6 11
7 profile7 3
8 profile8 10
9 profile9 16
10 profile10 13
11 profile11 12
12 profile12 7
13 profile13 6
14 profile14 9
15 profile15 4
16 profile16 8

我无法用 reshape 函数重现这个:

cprefm |>
(\(x) reshape(x, varying = 1:16, times = names(x)[1:16], timevar = "profile",
v.names = "values", direction = "long"))() |>
(\(x) head(x, 16))()

profile values id
1.profile1 profile1 14 1
2.profile1 profile1 16 2
3.profile1 profile1 7 3
4.profile1 profile1 9 4
5.profile1 profile1 7 5
6.profile1 profile1 14 6
7.profile1 profile1 3 7
8.profile1 profile1 2 8
9.profile1 profile1 1 9
10.profile1 profile1 4 10
11.profile1 profile1 4 11
12.profile1 profile1 14 12
13.profile1 profile1 7 13
14.profile1 profile1 14 14
15.profile1 profile1 10 15
16.profile1 profile1 4 16

我尝试了很多方法来修改 varying 参数,但我无法重现 tidyr::pivot_longer 的行为。请注意,即使 row.names 也有列名,但我也不喜欢那样。

我希望它看起来与 pivot_longer 相同。

最佳答案

1) 后处理问题中的 reshape 命令产生相同的输出,除了行名、行顺序和一个额外的 id 列,所以只需修复这些。

最后我们运行 pivot_longer 并将其输出转换为 data.frame 表明这与 reshape 的固定输出相同。

out <- reshape(cprefm, dir = "long", varying = names(cprefm), 
v.names = "value", timevar = "profile", times = names(cprefm))
out <- out[order(out$id), 1:2]
rownames(out) <- NULL

out.piv <- cprefm %>% pivot_longer(1:16, "profile", "rating")

identical(out, as.data.frame(out.piv))
## [1] TRUE

2) pre process w transpose 交替修复它before通过 reshape cprefm的转置将其传递给reshape。在这种情况下,我们只需选择所需的列,而无需排序即可按需要显示行顺序。

out2 <- reshape(as.data.frame(t(cprefm)), dir = "long",
varying = 1:nrow(cprefm), idvar = "profile", v.names = "value",
ids = names(cprefm), new.row.names = 1:prod(dim(cprefm)))[3:2]

identical(out2, as.data.frame(out.piv))
## [1] TRUE

3) as.data.frame.table w transpose 转置思想也适用于 as.data.frame.table:

out3 <- with(as.data.frame.table(t(cprefm), responseName = "value"),
data.frame(profile = as.character(Var1), value))

identical(out3, as.data.frame(out.piv))
## [1] TRUE

这个可以很好地写成这样的管道:

cprefm |>
t() |>
as.data.frame.table(responseName = "value") |>
with(data.frame(profile = as.character(Var1), value))

4) stack w tranpose and with stack:

out4 <- with(stack(as.data.frame(t(cprefm))), 
data.frame(profile = names(cprefm), value = values))

identical(out4, as.data.frame(out.piv))
## [1] TRUE

关于r - 如何在 base R 中通过 reshape 重现 pivot_longer 的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70328913/

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