79477948

Date: 2025-03-01 17:06:35
Score: 0.5
Natty:
Report link

Just for others that may want an extra bit of code.

I ended up being able to do it with ifelse() as well. It is less elegant than Grothendieck's solutions. However, in case someone finds it useful. I will leave it below.

#Example dataset
V1 <- c(1, NA, 6, 3, NA, NA, NA)
V2 <- c(1, NA, 6, 3, NA, NA, NA)
V3 <- c(NA, 3.5, NA, NA, 5, NA, NA)
V4 <- c(NA, 3.5, NA, NA, 5, NA, NA)
V5 <- c(NA, NA, NA, NA, NA, 5, 2)
V6 <- c(NA, NA, NA, NA, NA, 6, 1)


data <- data.frame(V1, V2, V3, V4, V5, V6)

#Creates the new grouping column
data$Group <- ifelse(!is.na(data$V1) & !is.na(data$V2), "G1",
                         ifelse(!is.na(data$V3) & !is.na(data$V4), "G2",
                                ifelse(!is.na(data$V5) & !is.na(data$V6), "G3", "Other")))
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: HelplessStatistician