rdplyrdata-management

Make a column based on conditions and previous columnb in R


I have a dataset like this :

structure(list(Q3.3 = c("Female", "Male", "Female", "Male", "Female", 
"Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male", 
"Female", "Female", "Male", "Female", "Male", "Male", "Female", 
"Male", "Male", "Male", "Female", "Male", "Male", "Female", "Male", 
"Female", "Male"), Q6.2 = c("Yes", "NE", "Yes", "NE", "Yes", 
"NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "No", "No", "NE", 
"Yes", "NE", "NE", "No", "NE", "NE", "NE", "No", "NE", "NE", 
"No", "NE", "Yes", "NE"), Q6.3 = c("3", "NE", "2", "NE", "1", 
"NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", 
"3", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", 
"NE", "2", "NE"), Q6.4 = c("Yes", "NE", "No", "NE", "No", "NE", 
"NE", "NE", "NE", "NE", "NE", "NE", "NE", "No", "No", "NE", "No", 
"NE", "NE", "No", "NE", "NE", "NE", "No", "NE", "NE", "No", "NE", 
"No", "NE"), Q6.5 = c("3", "NE", "NE", "NE", "NE", "NE", "NE", 
"NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", 
"NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", 
"NE"), Q7.5_1 = c("Yes", "", "Yes", "", "", "", "", "", "", "", 
"", "", "", "", "", "", "No, this was available, but I did not use it", 
"", "", "", "", "", "", "", "", "", "", "", "Yes", ""), dataname1 = c("Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", "Q", 
"Q", "Q", "Q"), Q7.5_1_bb = c("Yes", "NE", "Yes", "NE", "Yes", 
"NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "No", "No", "NE", 
"No, this was available, but I did not use it", "NE", "NE", "No", 
"NE", "NE", "NE", "No", "NE", "NE", "No", "NE", "Yes", "NE")), row.names = c(NA, 
30L), class = "data.frame")[![enter image description here][1]][1]

I want to mirror "NE" values to Q7.5_1 and also need to code "Q7.5_1" column again "NE"

If NOT Q6.3 = 2 Or Q6.3 = 3 Or Q6.3 = 4 Or Q6.3 = 5 Or Q6.3 = 6 Or Q6.3 = 7 Or Q6.3 = 8 Or Q6.3 = 9 Or Q6.3 = 10

Or If Q6.5 = 2 Or Q6.5 = 3 Or Q6.5 = 4 Or Q6.5 = 5 Or Q6.5 = 6 Or Q6.5 = 7 Or Q6.5 = 8 Or Q6.5 = 9 Or Q6.5 = 10

Or If Q6.3 = 1 And Q6.5 = 1

df<-df %>% mutate(Q7.5_1_bb = if_else(
  !(Q6.3 %in% c(2:10)) | Q6.5 %in% c(2:10) | Q6.3 == 1 & Q6.5 == 1,
  Q6.2, Q7.5_1))

When I used the above code, some of the values were weritten, like the row 5

    Q3.3 Q6.2 Q6.3 Q6.4 Q6.5 Q7.5_1  Q7.5_1_bb

1   Female  Yes 3   Yes 3   Yes     Yes
2   Male    NE  NE  NE  NE          NE
3   Female  Yes 2   No  NE  Yes     Yes
4   Male    NE  NE  NE  NE          NE
5   Female  Yes 1   No  NE          Yes

While I expect to see NE in Q7.5_1_bb in row 5.


Solution

  • Would it be this:

    newdf <- structure(list(Q3.3 = c("Female", "Male", "Female", "Male", "Female", 
                            "Male", "Male", "Male", "Male", "Male", "Male", "Male", "Male", 
                            "Female", "Female", "Male", "Female", "Male", "Male", "Female", 
                            "Male", "Male", "Male", "Female", "Male", "Male", "Female", "Male", 
                            "Female", "Male"), Q6.2 = c("Yes", "NE", "Yes", "NE", "Yes", 
                                                        "NE", "NE", "NE", "NE", "NE", "NE", "NE", "NE", "No", "No", "NE", 
                                                        "Yes", "NE", "NE", "No", "NE", "NE", "NE", "No", "NE", "NE", 
                                                        "No", "NE", "Yes", "NE"), Q6.3 = c("3", NA, "2", NA, "1", NA, 
                                                                                           NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "3", NA, NA, NA, NA, 
                                                                                           NA, NA, NA, NA, NA, NA, NA, "2", NA), Q6.4 = c("Yes", "", "No", 
                                                                                                                                          "", "No", "", "", "", "", "", "", "", "", "No", "No", "", "No", 
                                                                                                                                          "", "", "No", "", "", "", "No", "", "", "No", "", "No", ""), 
                   Q6.5 = c("3", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                            NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                            NA, NA, NA), Q7.5_1 = c("Yes", "", "Yes", "", "", "", "", 
                                                    "", "", "", "", "", "", "", "", "", "No, this was available, but I did not use it", 
                                                    "", "", "", "", "", "", "", "", "", "", "", "Yes", "")), row.names = c(NA, 
         
    # Conditional
    newdf2 <- newdf %>% mutate(Q7.5_1_bb = case_when(
      !(Q6.3 %in% c(2:10)) ~ if_else(Q6.3 == 1, 
                                if_else(Q6.5 == 1, Q7.5_1, "NE"), Q7.5_1),
        Q6.5 %in% c(2:10) ~ Q7.5_1,
      TRUE ~ "NE"
      )
      )