【问题标题】:Combining several columns based on matching text in R根据R中的匹配文本组合几列
【发布时间】:2026-01-28 16:50:01
【问题描述】:

我在 Qualtrics 中进行了 4 个条件的研究。为方便起见,我在下面的示例中仅包含 3 个。结果数据如下所示:

condition  Q145   Q243   Q34    Q235   Q193   Q234   Q324   Q987   Q88         
condition  How a? How b? How c? How a? How b? How c? How a? How b? How c? 
1          3      5      2
1          5      4      7
1          3      1      4
2                               3      4      7
2                               1      2      8
2                               1      3      9
3                                                     7      6      5
3                                                     8      1      3
3                                                     9      2      2

第 2 行的问题在实际数据集中更长、更复杂,但它们在不同条件下是一致的。在这个示例中,我试图捕捉一致性以及默认变量名称(均以 Q 开头)不匹配的事实。

最终,我想要一个如下所示的数据框。我想将一个问题的所有回答合并到每个问题的一栏中。 (然后我会进去重命名冗长的问题,用更简洁的变量名和“整理”数据。)

condition  How a? How b? How c? 
1          3      5      2
1          5      4      7
1          3      1      4
2          3      4      7
2          1      2      8
2          1      3      9
3          7      6      5
3          8      1      3
3          9      2      2

如果您有任何关于如何实现此目的的想法,我将不胜感激。

【问题讨论】:

  • 请使用dput()分享您的数据。
  • 数据本身太复杂,无法使用dput()。我已经尝试创建原始数据框的可重现版本,但不确定如何解决大的差距......因为每行有 6 个空列,我该怎么办?我正在尝试使用以下内容: x
  • 请不要使用评论部分添加任何类型的数据。你应该搜索“wide to long r”,就是这样的问题。我猜你会用tidyr::gather解决问题。
  • 澄清一下,我并没有尝试在评论中添加数据。我试图询问如何创建可重现的数据版本,然后根据 markus 的要求将其添加到问题中。

标签: r tidyverse qualtrics


【解决方案1】:
library(tidyverse)

file = 'condition,Q145  ,Q243  ,Q34   ,Q235  ,Q193  ,Q234  ,Q324  ,Q987  ,Q88
        condition,How a?,How b?,How c?,How a?,How b?,How c?,How a?,How b?,How c?
        1        ,3     ,5     ,2     ,      ,      ,      ,      ,      ,
        1        ,5     ,4     ,7     ,      ,      ,      ,      ,      ,
        1        ,3     ,1     ,4     ,      ,      ,      ,      ,      ,
        2        ,      ,      ,      ,3     ,4     ,7     ,      ,      ,
        2        ,      ,      ,      ,1     ,2     ,8     ,      ,      ,
        2        ,      ,      ,      ,1     ,3     ,9     ,      ,      ,
        3        ,      ,      ,      ,      ,      ,      , 7    , 6    , 5
        3        ,      ,      ,      ,      ,      ,      , 8    , 1    , 3
        3        ,      ,      ,      ,      ,      ,      , 9    , 2    , 2'

# Read in just the data without the weird header situation
data <- read_csv(file, col_names = FALSE, skip = 2)

# Pull out the questions row and reshape into a dataframe to make the next part easy
questions <- gather(read_csv(file, col_names = FALSE, skip = 1, n_max = 1))

# Generate list of data frames (one df for each question)
split(questions, questions$value) %>%
  # Then coalesce the columns
  map_df(~do.call(coalesce, data[, .x$key]))

给出以下结果:

# A tibble: 9 x 4
  condition `How a?` `How b?` `How c?`
      <int>    <int>    <int>    <int>
1         1        3        5        2
2         1        5        4        7
3         1        3        1        4
4         2        3        4        7
5         2        1        2        8
6         2        1        3        9
7         3        7        6        5
8         3        8        1        3
9         3        9        2        2

当然,如果您最终打算改用长格式,您可以这样做:

data %>%
  gather(key, answer, -X1) %>%
  filter(!is.na(answer)) %>%
  left_join(questions, by = 'key') %>%
  select(condition = X1, question = value, answer)

结果如下:

# A tibble: 27 x 3
   condition question answer
       <int> <chr>     <int>
 1         1 How a?        3
 2         1 How a?        5
 3         1 How a?        3
 4         1 How b?        5
 5         1 How b?        4
 6         1 How b?        1
 7         1 How c?        2
 8         1 How c?        7
 9         1 How c?        4
10         2 How a?        3
# ... with 17 more rows

【讨论】: