【问题标题】:How to unpivot specific columns in R dataframe如何取消透视 R 数据框中的特定列
【发布时间】:2021-10-24 02:25:31
【问题描述】:

我有一个 R 格式的数据框,如下表第一个所示。我想将列“M1.1”、“M1.2”和“M1.3”合并到单个列“M1”中,以便条目位于它们自己的行上(其他列中的 ID 和值会重复)如第二个表所示。我可以使用哪些功能来完成此操作?

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

【问题讨论】:

标签: r reshape unpivot data-wrangling


【解决方案1】:

我们可以使用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"
))

【讨论】:

  • @AnoushiravanR 你可以使用tribble,但你需要一些手动操作,这有点烦人。在这种情况下,您可以点击Edit 按钮并复制所示表格的源代码。接下来使用readr::read_delim([COPY-PASTE-CODE-in-""], delim = "|", trim_ws = TRUE)。现在您只需要删除两列。
  • 对不起,我瞎了 :) Doublevoted!
  • @AnoushiravanR 最佳实践:等待像 TarJae 这样的人手动导入数据并提供dput()-结构。 ;-)
  • @TarJae 你是对的,它很好用——我的错!感谢您的帮助!
  • @ava。请看这里:stackoverflow.com/questions/5171593/…>。我的猜测是您的新数据一定很大?尝试使用此处给出的答案进行管理并告诉我。
【解决方案2】:

使用@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

【讨论】:

    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    相关资源
    最近更新 更多