【问题标题】:How to repetitively compute sums of groups of columns and store them as new columns in R?如何重复计算列组的总和并将它们存储为 R 中的新列?
【发布时间】:2022-01-07 00:22:56
【问题描述】:

我想计算我的数据集中多个分组列的总和并将这些总和存储为新列,但我不知道如何用R 语言对其进行编码。

设置: 100 名参与者玩了一个视频游戏,并使用标准化问卷对各个方面进行了评分。数据是宽格式的,所以我有 100 行(每个参与者一行)和一定数量的列,这些列与被评分的视频游戏的各个方面有关。

为了展示我的数据集的样子,this image 给出了它的简化版本。

详情: 因为问卷包含三个不同的分量表,我需要汇总所有参与者对每个分量表的个人评分(在the image 中突出显示绿色、黄色、蓝色)。每个分量表的总和必须添加为一个新列(导致三个分量表的总和)。 the image 中的红色列说明了这一点。

我不知道如何用R 语言有效地对此进行编码。谁能提供一些方向?

【问题讨论】:

标签: r for-loop data-analysis


【解决方案1】:

下次您提出问题时,请提供一个可重现的示例(正如@Jon Spring 也评论的那样)。现在,我将提供一个。

假设您提供的图像中的列名与您自己数据中的列名相似,并且同样重要的是,例如subscale_1 中要求和的绿色列始终对应于 Buttons_ 的第 1、第 3 和第 6 列,依此类推,以下代码行应该可以实现您要查找的内容(尽管我确信还有更多实现这一目标的有效方法)。

首先,让我们创建一个reprex,我们可以用它来测试我们的代码。

set.seed(1)

# no. of participants
n <- 100L

# columns in the desired format
n_cols <- 21L
df <- data.frame(matrix(rep(NA, times = n * n_cols), ncol = n_cols, byrow = TRUE))
names(df) <- c('id', paste0('LoadScreen_Buttons_', 1:7),
               paste0('LoadScreen_Subscale_', 1:3), paste0('GameHUD_Buttons_', 1:7),
               paste0('GameHUD_Buttons_Subscale_', 1:3))

# subject id
df$id <- 1L:100L

# allocate random values to the buttons columns
buttons_cols <- grep('ns_[[:digit:]]', names(df), value = TRUE)
df[, buttons_cols] <- apply(df[, buttons_cols], 2, \(x) sample(1L:7L, n, replace = TRUE))

df的结构(对于前两个参与者的以'LoadScreen'开头的列名)如下。

> head(df[, c('id', grep('^L', names(df), value = TRUE))], 2)
  id LoadScreen_Buttons_1 LoadScreen_Buttons_2 LoadScreen_Buttons_3 LoadScreen_Buttons_4 LoadScreen_Buttons_5
1  1                    1                    1                    2                    7                    6
2  2                    4                    7                    2                    4                    6
  LoadScreen_Buttons_6 LoadScreen_Buttons_7 LoadScreen_Subscale_1 LoadScreen_Subscale_2 LoadScreen_Subscale_3
1                    4                    5                    NA                    NA                    NA
2                    5                    6                    NA                    NA                    NA

现在我们需要提取dfsubscale 列的名称。我们还需要提取第 1、第 3 和第 6 个bottons 列(对应于图像中的绿色列),取其中的rowSums,并将结果存储在与特定@ 相关的subscale 列中987654332@ 列。这同样适用于您提供的图像中的黄色和蓝色列。最后,因为我们有 ibuttons 列(i = 2 在这个reprex,即我们有LoadScreenGameHUD),我们迭代这使用三个 for 循环处理 i 次,每种颜色一个循环。

# compute buttons subscales -----------------

# extract column names from df that include the buttoms columns
buttons <- grep('ns_[[:digit:]]', names(df), value = TRUE)

# extract column names from df that include the subscales
subscales <- grep('e_[[:digit:]]$', names(df), value = TRUE)

# green ------------
green <- grep('1$|3$|6$', buttons, value = TRUE)
mat <- matrix(green, ncol = length(green)/3L)
for(i in 1:dim(mat)[2]) {
  df[, subscales[endsWith(subscales, '1')][i]] <- rowSums(df[, mat[, i]])
}
# yellow ------------
yellow <- grep('2$|7$', buttons, value = TRUE)
mat <- matrix(yellow, ncol = length(yellow)/2L)
for(i in 1:dim(mat)[2]) {
  df[, subscales[endsWith(subscales, '2')][i]] <- rowSums(df[, mat[, i]])
}
# blue ------------
blue <- grep('4$|5$', buttons, value = TRUE)
mat <- matrix(blue, ncol = length(blue)/2L)
for(i in 1:dim(mat)[2]) {
  df[, subscales[endsWith(subscales, '3')][i]] <- rowSums(df[, mat[, i]])
}

结果。

> head(df[, c('id', grep('^L', names(df), value = TRUE))], 2)
  id LoadScreen_Buttons_1 LoadScreen_Buttons_2 LoadScreen_Buttons_3 LoadScreen_Buttons_4 LoadScreen_Buttons_5
1  1                    1                    1                    2                    7                    6
2  2                    4                    7                    2                    4                    6
  LoadScreen_Buttons_6 LoadScreen_Buttons_7 LoadScreen_Subscale_1 LoadScreen_Subscale_2 LoadScreen_Subscale_3
1                    4                    5                     7                     6                    13
2                    5                    6                    11                    13                    10

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 2021-02-03
    • 2017-01-13
    • 1970-01-01
    相关资源
    最近更新 更多