【问题标题】:Reshaping a dataframe by moving part of an existing row to a new row通过将现有行的一部分移动到新行来重塑数据框
【发布时间】:2018-05-10 12:20:36
【问题描述】:

我有以下格式的数据:

structure(list(choice = structure(c(1L, 1L, 2L, 1L), .Label = c("option1", 
"option2"), class = "factor"), option1var1 = structure(c(1L, 
1L, 1L, 1L), .Label = "A", class = "factor"), option1var2 = structure(c(1L, 
1L, 1L, 2L), .Label = c("B", "H"), class = "factor"), option2var1 = structure(c(1L, 
1L, 2L, 3L), .Label = c("C", "F", "I"), class = "factor"), option2var2 = structure(1:4, .Label = c("D", 
"E", "G", "K"), class = "factor")), .Names = c("choice", "option1var1", 
"option1var2", "option2var1", "option2var2"), class = "data.frame", row.names = c(NA, 
-4L))

有六列。第一列包含受访者 ID,第二列包含有关受访者做出的选择的数据(选项 1 或选项 2),第 3 列和第 4 列包含与选项 1 关联的属性,第 4 列和第 5 列包含与选项 2 关联的属性。

我想转换数据框,使其看起来像这样:

structure(list(respondent = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), 
    choice = c(1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L), option = structure(c(1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("option1", "option2"
    ), class = "factor"), var1 = structure(c(1L, 2L, 1L, 2L, 
    1L, 3L, 1L, 4L), .Label = c("A", "C", "F", "I"), class = "factor"), 
    var2 = structure(c(1L, 2L, 1L, 3L, 1L, 4L, 5L, 6L), .Label = c("B", 
    "D", "E", "G", "H", "K"), class = "factor")), .Names = c("respondent", 
"choice", "option", "var1", "var2"), class = "data.frame", row.names = c(NA, 
-8L))

这需要将每行一分为二,将 option1 数据保留在一行中,将 option2 数据移动到另一行,并创建一个新的数值变量,其中包含有关哪个选项(选项 1 或选项 2 每个受访者选项)的信息。

似乎没有关于这种类型的转换的任何信息——无论是在此处还是在我找到的 R 文档中。有谁知道怎么做?

【问题讨论】:

    标签: r dataframe dplyr tidyr


    【解决方案1】:

    假设原始数据框为df1,最终输出为df2

    library(tidyverse)
    
    df2 <- df1 %>%
      mutate(respondent = 1:n()) %>%
      gather(Option, Value, starts_with("option")) %>%
      separate(Option, into = c("option", "Var"), sep = 7) %>%
      mutate(choice = ifelse(choice == option, 1L, 0L)) %>%
      spread(Var, Value) %>%
      select(respondent, choice, option, starts_with("var")) %>%
      arrange(respondent, option)
    df2
    #   respondent choice  option var1 var2
    # 1          1      1 option1    A    B
    # 2          1      0 option2    C    D
    # 3          2      1 option1    A    B
    # 4          2      0 option2    C    E
    # 5          3      0 option1    A    B
    # 6          3      1 option2    F    G
    # 7          4      1 option1    A    H
    # 8          4      0 option2    I    K
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2021-12-11
      • 2022-01-16
      相关资源
      最近更新 更多