如何取消透视 R 数据框中的特定列
How to unpivot specific columns in R dataframe
我有一个 R 格式的数据框,如下面第一个 table 所示。我想将“M1.1”、“M1.2”和“M1.3”列合并到一个“M1”列中,以便条目位于它们自己的行中(其他列中的 id 和值将重复)如第二个table所示。我可以使用什么功能来完成此操作?
id
M1.1
M1.2
M1.3
M2
M3
M4
M5
M6
test a
test t
test a
test y
test
test t
test y
test u
test w
test s
test r
test a
test h
test r
test j
test j
test w
test d
id
M1
M2
M3
M4
M5
M6
test a
test t
test
test t
test y
test u
test w
test a
test a
test
test t
test y
test u
test w
test a
test y
test
test t
test y
test u
test w
test s
test r
test r
test j
test j
test w
test d
test s
test a
test r
test j
test j
test w
test d
test s
test h
test r
test j
test j
test w
test d
我们可以使用 pivot_longer
:
library(dplyr)
library(tidyr)
df %>%
pivot_longer(
cols = c(M1.1, M1.2, M1.3),
names_to = "names",
values_to = "M1"
) %>%
select(id, M1, M2:M6)
A tibble: 6 x 7
id M1 M2 M3 M4 M5 M6
<chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 test a test t test test t test y test u test w
2 test a test a test test t test y test u test w
3 test a test y test test t test y test u test w
4 test s test r test r test j test j test w test d
5 test s test a test r test j test j test w test d
6 test s test h test r test j test j test w test d
数据:
structure(list(id = c("test a", "test s"), M1.1 = c("test t",
"test r"), M1.2 = c("test a", "test a"), M1.3 = c("test y", "test h"
), M2 = c("test", "test r"), M3 = c("test t", "test j"), M4 = c("test y",
"test j"), M5 = c("test u", "test w"), M6 = c("test w", "test d"
)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
))
使用@TarJae 数据,大概更快data.table
:
library(data.table)
dat <- data.table(dat)
melt(dat, , paste('M1', 1:3, sep = '.'), , 'M1')[
order(id),
c('id', paste('M', 1:6, sep = ''))
]
# id M1 M2 M3 M4 M5 M6
# 1: test a test t test test t test y test u test w
# 2: test a test a test test t test y test u test w
# 3: test a test y test test t test y test u test w
# 4: test s test r test r test j test j test w test d
# 5: test s test a test r test j test j test w test d
# 6: test s test h test r test j test j test w test d
我有一个 R 格式的数据框,如下面第一个 table 所示。我想将“M1.1”、“M1.2”和“M1.3”列合并到一个“M1”列中,以便条目位于它们自己的行中(其他列中的 id 和值将重复)如第二个table所示。我可以使用什么功能来完成此操作?
id | M1.1 | M1.2 | M1.3 | M2 | M3 | M4 | M5 | M6 |
---|---|---|---|---|---|---|---|---|
test a | test t | test a | test y | test | test t | test y | test u | test w |
test s | test r | test a | test h | test r | test j | test j | test w | test d |
id | M1 | M2 | M3 | M4 | M5 | M6 |
---|---|---|---|---|---|---|
test a | test t | test | test t | test y | test u | test w |
test a | test a | test | test t | test y | test u | test w |
test a | test y | test | test t | test y | test u | test w |
test s | test r | test r | test j | test j | test w | test d |
test s | test a | test r | test j | test j | test w | test d |
test s | test h | test r | test j | test j | test w | test d |
我们可以使用 pivot_longer
:
library(dplyr)
library(tidyr)
df %>%
pivot_longer(
cols = c(M1.1, M1.2, M1.3),
names_to = "names",
values_to = "M1"
) %>%
select(id, M1, M2:M6)
A tibble: 6 x 7
id M1 M2 M3 M4 M5 M6
<chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 test a test t test test t test y test u test w
2 test a test a test test t test y test u test w
3 test a test y test test t test y test u test w
4 test s test r test r test j test j test w test d
5 test s test a test r test j test j test w test d
6 test s test h test r test j test j test w test d
数据:
structure(list(id = c("test a", "test s"), M1.1 = c("test t",
"test r"), M1.2 = c("test a", "test a"), M1.3 = c("test y", "test h"
), M2 = c("test", "test r"), M3 = c("test t", "test j"), M4 = c("test y",
"test j"), M5 = c("test u", "test w"), M6 = c("test w", "test d"
)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
))
使用@TarJae 数据,大概更快data.table
:
library(data.table)
dat <- data.table(dat)
melt(dat, , paste('M1', 1:3, sep = '.'), , 'M1')[
order(id),
c('id', paste('M', 1:6, sep = ''))
]
# id M1 M2 M3 M4 M5 M6
# 1: test a test t test test t test y test u test w
# 2: test a test a test test t test y test u test w
# 3: test a test y test test t test y test u test w
# 4: test s test r test r test j test j test w test d
# 5: test s test a test r test j test j test w test d
# 6: test s test h test r test j test j test w test d