多值列的 dplyr 中的 Pivot_longer
Pivot longer in dplyr for mutiple value columns
我有以下 table 可以使用此代码生成
structure(list(total = c(9410, 12951.1794783802), op = c(3896.66666666667,
6976.57663230241), ox = c(2200, 4920.84776902887), ox15 = c(183.333333333333,
694.262648008611), ox30 = c(133.333333333333, 368.090117767537
), hy = c(283.333333333333, 1146.14924596984), hy10 = c(NA, 433.993925588459
)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
))
虽然当前有 2 行和 7 列,但我希望生成的 table 有 7 行和 3 列,每个当前行中的值都将转置到它自己的列中。有没有办法使用 pivot_longer() 函数来做到这一点?
这样怎么样:
library(tidyverse)
dat <- structure(list(total = c(9410, 12951.1794783802),
op = c(3896.66666666667, 6976.57663230241),
ox = c(2200, 4920.84776902887),
ox15 = c(183.333333333333, 694.262648008611),
ox30 = c(133.333333333333, 368.090117767537),
hy = c(283.333333333333, 1146.14924596984),
hy10 = c(NA, 433.993925588459)),
row.names = c(NA, -2L),
class = c("tbl_df", "tbl", "data.frame"))
dat %>%
mutate(obs = 1:n()) %>%
pivot_longer(-obs, names_to="var", values_to="vals") %>%
pivot_wider(names_from="obs", values_from="vals", names_prefix="obs_")
#> # A tibble: 7 × 3
#> var obs_1 obs_2
#> <chr> <dbl> <dbl>
#> 1 total 9410 12951.
#> 2 op 3897. 6977.
#> 3 ox 2200 4921.
#> 4 ox15 183. 694.
#> 5 ox30 133. 368.
#> 6 hy 283. 1146.
#> 7 hy10 NA 434.
由 reprex package (v2.0.1)
创建于 2022-02-03
我不认为 pivot_longer
是这里的正确功能。您可以通过 t
转置并使用 rownames_to_column
来更简单地实现它
tibble::rownames_to_column(as.data.frame(t(df)))
#> rowname V1 V2
#> 1 total 9410.0000 12951.1795
#> 2 op 3896.6667 6976.5766
#> 3 ox 2200.0000 4920.8478
#> 4 ox15 183.3333 694.2626
#> 5 ox30 133.3333 368.0901
#> 6 hy 283.3333 1146.1492
#> 7 hy10 NA 433.9939
由 reprex package (v2.0.1)
创建于 2022-02-03
这可以通过 data.table::transpose
实现
data.table::transpose(df1, keep.names = "var")
var V1 V2
1 total 9410.0000 12951.1795
2 op 3896.6667 6976.5766
3 ox 2200.0000 4920.8478
4 ox15 183.3333 694.2626
5 ox30 133.3333 368.0901
6 hy 283.3333 1146.1492
7 hy10 NA 433.9939
或使用sjmisc::rotate_df
:
sjmisc::rotate_df(df, rn="id")
#> id V1 V2
#> 1 total 9410.0000 12951.1795
#> 2 op 3896.6667 6976.5766
#> 3 ox 2200.0000 4920.8478
#> 4 ox15 183.3333 694.2626
#> 5 ox30 133.3333 368.0901
#> 6 hy 283.3333 1146.1492
#> 7 hy10 NA 433.9939
我有以下 table 可以使用此代码生成
structure(list(total = c(9410, 12951.1794783802), op = c(3896.66666666667,
6976.57663230241), ox = c(2200, 4920.84776902887), ox15 = c(183.333333333333,
694.262648008611), ox30 = c(133.333333333333, 368.090117767537
), hy = c(283.333333333333, 1146.14924596984), hy10 = c(NA, 433.993925588459
)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
))
虽然当前有 2 行和 7 列,但我希望生成的 table 有 7 行和 3 列,每个当前行中的值都将转置到它自己的列中。有没有办法使用 pivot_longer() 函数来做到这一点?
这样怎么样:
library(tidyverse)
dat <- structure(list(total = c(9410, 12951.1794783802),
op = c(3896.66666666667, 6976.57663230241),
ox = c(2200, 4920.84776902887),
ox15 = c(183.333333333333, 694.262648008611),
ox30 = c(133.333333333333, 368.090117767537),
hy = c(283.333333333333, 1146.14924596984),
hy10 = c(NA, 433.993925588459)),
row.names = c(NA, -2L),
class = c("tbl_df", "tbl", "data.frame"))
dat %>%
mutate(obs = 1:n()) %>%
pivot_longer(-obs, names_to="var", values_to="vals") %>%
pivot_wider(names_from="obs", values_from="vals", names_prefix="obs_")
#> # A tibble: 7 × 3
#> var obs_1 obs_2
#> <chr> <dbl> <dbl>
#> 1 total 9410 12951.
#> 2 op 3897. 6977.
#> 3 ox 2200 4921.
#> 4 ox15 183. 694.
#> 5 ox30 133. 368.
#> 6 hy 283. 1146.
#> 7 hy10 NA 434.
由 reprex package (v2.0.1)
创建于 2022-02-03我不认为 pivot_longer
是这里的正确功能。您可以通过 t
转置并使用 rownames_to_column
tibble::rownames_to_column(as.data.frame(t(df)))
#> rowname V1 V2
#> 1 total 9410.0000 12951.1795
#> 2 op 3896.6667 6976.5766
#> 3 ox 2200.0000 4920.8478
#> 4 ox15 183.3333 694.2626
#> 5 ox30 133.3333 368.0901
#> 6 hy 283.3333 1146.1492
#> 7 hy10 NA 433.9939
由 reprex package (v2.0.1)
创建于 2022-02-03这可以通过 data.table::transpose
data.table::transpose(df1, keep.names = "var")
var V1 V2
1 total 9410.0000 12951.1795
2 op 3896.6667 6976.5766
3 ox 2200.0000 4920.8478
4 ox15 183.3333 694.2626
5 ox30 133.3333 368.0901
6 hy 283.3333 1146.1492
7 hy10 NA 433.9939
或使用sjmisc::rotate_df
:
sjmisc::rotate_df(df, rn="id")
#> id V1 V2
#> 1 total 9410.0000 12951.1795
#> 2 op 3896.6667 6976.5766
#> 3 ox 2200.0000 4920.8478
#> 4 ox15 183.3333 694.2626
#> 5 ox30 133.3333 368.0901
#> 6 hy 283.3333 1146.1492
#> 7 hy10 NA 433.9939