首页 > 解决方案 > 使用嵌套数据循环遍历行和列

问题描述

我有以下数据结构:小组会议。这些小组开会的频率不同,每次会议的小组成员数量也各不相同。

 $ GroupID                    : chr  "1" "1" "1" "1" ...
 $ groupnames                 : chr  "A&M" "A&M" "A&M" "A&M" ...
 $ MeetiID                    : chr  "1" "1" "2" "2" ...
 $ Date_Meetings              : chr  "43293" "43293" "43298" "43298" ...
 $ PersonID                   : num  171 185 171 185 185 113 135 113 135 113 ...
 $ v_165                      : chr  "3" "3" "4" "3" ...
 $ v_166                      : chr  "2" "2" "3" "3" ...
 $ v_167                      : chr  "2" "4" "4" "3" ...
 $ v_168                      : chr  "6" "7" "4" "5" ...
 $ problemtypes_categories: chr  "Knowledgeproblem" "Knowledgeproblem" "Motivationalproblem" "Coordinationproblem" ...
 $ v_165_dicho                : num  0 0 0 0 1 1 1 0 0 1 ...
 $ v_166_dicho                : num  0 0 0 0 0 0 0 0 0 0 ...
 $ v_167_dicho                : num  0 0 0 0 1 1 0 0 0 0 ...

现在我必须创建一个新变量,该变量应该是二进制 (0/1),名称为 agreement_levels。因此,每次,一个小组中的一个人 - 关于同一个学习会议 - 与同一小组的其他学习者在同一个会议上具有相同的问题类型类别,两个学习者(或三个或四个,取决于各会议的小组规模)应在协议变量处获得值 1,否则他们都应为 0。每当一个人(例如,四个学习者中)已经有与其他人不同的问题类别时,就会有一个 0所有人的协议变量。如果同一会议的数据集中只有 1 人,则同意时必须有 NA。但是,当一个人的问题类型变量为 NA,并且同一会议的数据集中有 2 个人时,两者都在同意时得到 0;

我确实已经写了一个命令,但它还没有工作,仍然不考虑 NA:

 GroupID1 <- df$GroupID[1:nrow,]
                         TreffID1 <- df$TreffID[1:nrow,]
                         for(i in 1:(GroupID1 -1){
                           for(j in 1:(TreffID1 -1){
                             if(df[i, 3] == df[i+1, 3]-1){
                                  if(df[i, 15] == df[i+1, 15]-1){
                                      df[c(i, i+1), 28] <- 1,
                                      df[c(i, i+1), 28] <- 0

提前谢谢了。

dput(head(df))
structure(list(GroupID = c("1", "1", "1", "1", "1", "2"), TreffID = c("1", "1", 
"2", "2", "3", "1"), PersonID = c(171, 185, 171, 185, 
185, 113), problemtypen_oberkategorien = c("Verständnisprobleme", 
"Verständnisprobleme", "Motivationsprobleme", "Motivationsprobleme", 
"Motivationsprobleme", "Motivationsprobleme"), passung.exkl = c("0", 
"0", "0", "0", "1", "1")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

标签: rloopsfor-loopif-statementnested

解决方案


我使用 R 代替循环,而不是循环dplyr。我不确定我的逻辑是否正确,因为那里有很多。例如,您没有指定 NA 问题类型和 3 个人会发生什么。但这里是使用的起点group_by,因此您正在查看具有相同 GroupID 和 TreffID 的每组行,然后是mutateand case_when,根据条件将值分配给新列,然后类似n()的函数计算多少行并且n_distinct计算不同的行,所以如果它是 ==1 那么我们知道它们都是相同的。

    library(tidyverse)
    df <- df %>% 
      group_by(GroupID, TreffID) %>% 
      mutate(agreement_levels = case_when(n() == 1 ~ -1,
                                          is.na(problemtypen_oberkategorien) & n() == 2 ~ 0,
                                          is.na(problemtypen_oberkategorien) & n() > 2 ~ -1,
                                          n_distinct(problemtypen_oberkategorien, na.rm = FALSE) == 1 ~ 1,
                                          n_distinct(problemtypen_oberkategorien, na.rm = FALSE) > 1 ~ 0,
                                          TRUE ~ -1),
             agreement_levels = na_if(agreement_levels, -1)) %>%
      select(GroupID, TreffID, problemtypen_oberkategorien, agreement_levels, everything()) 

推荐阅读