mardi 27 avril 2021

Creating New Dataframe Based on Cells of Other Dataframes using For loop R

I have 2 data frames with the same row and column structure that both have a lot of NA values. I want to create another data frame that simply tells me which cells in the 2 original data frames actually have values. For example

enter image description here So far I have been able to do this manually by mutating a series of if else statements for each column like this:

combined <- trial_1[,1:2] %>%
   mutate("Part1" = ifelse(!is.na(trial_1$Part1) & !is.na(trial_2$Part1), "1 & 2",
 ifelse(!is.na(trial_1$Part1) & is.na(trial_2$Part1), "1 only", ifelse(is.na(trial_1$Part1) & !is.na(trial_2$Part1),
 "2 only", ifelse(is.na(trial_1$Part1) & is.na(trial_2$Part1),
 "NA", "Failed"))))) %>%

   mutate("Part2" = ifelse(!is.na(trial_1$Part2) & !is.na(trial_2$Part2),
 "1 & 2",ifelse(!is.na(trial_1$Part2) & is.na(trial_2$Part2), "1 only",
 ifelse(is.na(trial_1$Part2) & !is.na(trial_2$Part2), "2 only", ifelse(is.na(trial_1$Part2) & is.na(trial_2$Part2), "NA", "Failed"))))) %>%

   mutate("Part3" = ifelse(!is.na(trial_1$Part3) & !is.na(trial_2$Part3), "1 & 2",
 ifelse(!is.na(trial_1$Part3) & is.na(trial_2$Part3),
 "1 only", ifelse(is.na(trial_1$Part3) & !is.na(trial_2$Part3), "2 only", ifelse(is.na(trial_1$Part3) & is.na(trial_2$Part3),
 "NA", "Failed"))))) %>%

   mutate("Part4" = ifelse(!is.na(trial_1$Part4) & !is.na(trial_2$Part4),
 "1 & 2", ifelse(!is.na(trial_1$Part4) & is.na(trial_2$Part4), "1 only", ifelse(is.na(trial_1$Part4) & !is.na(trial_2$Part4),
 "2 only", ifelse(is.na(trial_1$Part4) & is.na(trial_2$Part4), "NA", "Failed")))))

But this is obviously not efficient so I tried using a for loop, which does not work:

participants <- list('Part1', 'Part2', 'Part3', 'Part4')

combined <- trial_1[,1:2]

for (i in participants) {

combined <- combined %>%
  mutate(i = ifelse(!is.na(trial_1$i) & !is.na(trial_2$i), "1 & 2",
 ifelse(!is.na(trial_1$i) & is.na(trial_2$i), "1 only",
 ifelse(is.na(trial_1$i) & !is.na(trial_2$i), "2 only",
 ifelse(is.na(trial_1$i) & is.na(trial_2$i), "NA", "Failed")))))

}

Any help on how to restructure this for loop, which I think is the way to go, would be very helpful. Thanks!

Aucun commentaire:

Enregistrer un commentaire