mercredi 27 février 2019

Loop across rows and colums with nested data

I have the following data structure: Meetings in Persons in Groups. The groups met differently often and the number of group members varied for every meeting.

 $ 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 ...

Now I have to create a new variable that should be binary (0/1) with the name agreement_levels. So, every time, a person in one group has - regarding the same learning meeting - a same problem type category than the other learner(s) of the same group at the same meeting, both learners (or three or four, depending on the group size for a respective meeting) should get the value 1 at the agreement variable, else they should all get 0. Whenever a person (e.g., among four learners) already has a different category of problem than the others, there is a 0 on the agreement variable for all. If only 1 person is in the data set for one and the same meeting, there must be a NA at agree. When one person has NA at the problemtype variable, however, and there are 2 people in the data set for the same meeting, both get 0 at agree; but if there are 4 people for the same meeting in the data set and one of them has NA at problemtype, then only this person but not the others get NA at agree.

I did already write a command, but it is not working yet and still does not consider the NAs:

 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

Many thanks in advance.

dput(head(df))
structure(list(GroupID = c("1", "1", "1", "1", "1", "2"), v_187_corr = c("A&M", 
"A&M", "A&M", "A&M", "A&M", "Adornos"), TreffID = c("1", "1", 
"2", "2", "3", "1"), Datum.Treffen = c("43293", "43293", "43298", 
"43298", "43303", "43269"), PersonID = c(171, 185, 171, 185, 
185, 113), `Number participants per group` = c(2, 2, 2, 2, 2, 
2), lfdn = c(784, 858, 867, 866, 1008, 395), v_162 = c("L", "L", 
"K", "K", "M", "K"), v_163 = c("Schuldrecht und Anspruchsgrundlagen sehr umfangreich", 
"Schuldrecht ist sehr umfangreich", "Wir haben am gleichen Tag Klausur geschrieben und danach war die Motivation und Konzentration nicht mehr voll da.", 
"Direkt nach der Prüfung wieder zu lernen ist schwierig", "Der Lernstoff war  sehr langweilig uns somit sehr zäh zu erlernen", 
"Wir brauchten lange, bis wir richtig angefangen haben, da unsere Motivation heute nicht wirklich groß war. Wir sind deshalb auch nicht wirklich schnell vorangekommen."
), v_165 = c("3", "3", "4", "3", "5", "5"), v_166 = c("2", "2", 
"3", "3", "1", "4"), v_167 = c("2", "4", "4", "3", "5", "5"), 
v_164 = c("Fälle üben", "Prüfungsvorbereitung", "Vorbereitung auf die nächste Klausur.", 
"Wiederholung des Stoffea", "Klausurvorbereitung", "Wir wollten das erste Kapitel durchnehmen. Wir haben es auch geschafft."
), v_168 = c("6", "7", "4", "5", "6", "5"), problemtypen_oberkategorien = c("Verständnisprobleme", 
"Verständnisprobleme", "Motivationsprobleme", "Motivationsprobleme", 
"Motivationsprobleme", "Motivationsprobleme"), passung.exkl = c("0", 
"0", "0", "0", "1", "1"), passung.inkl = c("1", "0", "0", 
"0", "0", "1"), zPassng = c("0.5", "-0.5", "-0.5", "-0.5", 
"-0.5", "0.5"), zPassng.std = c("0.60342314080027204", "-1.6551034719093201", 
"-1.6551034719093201", "-1.6551034719093201", "-1.6551034719093201", 
"0.60342314080027204"), zZufri = c("0.82498531022632304", 
"1.5538558270282199", "-0.63275572337746999", "9.6114793424426206E-2", 
"0.82498531022632304", "9.6114793424426206E-2"), zprobtyp = c("-0.78941313079864905", 
"-0.78941313079864905", "1.2651521698892201", "1.2651521698892201", 
"1.2651521698892201", "1.2651521698892201"), prb_pass = c("-0.47635015077549703", 
"1.3065604135556499", "-2.0939577488772598", "-2.0939577488772598", 
"-2.0939577488772598", "0.76342209594483601"), v_165_dicho = c(0, 
0, 0, 0, 1, 1), v_166_dicho = c(0, 0, 0, 0, 0, 0), v_167_dicho = c(0, 
0, 0, 0, 1, 1), agree_group_withinmeet_levelrat = c("1", 
"1", "1", "1", "NA", "0"), agree_probtyp_ja_nein = c("1", 
"1", "1", "1", "NA", "1"), agree_group_withinmeet_overall = c("1", 
"1", "1", "1", "NA", "0")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

Aucun commentaire:

Enregistrer un commentaire