0

I want to create a new variable in which "Yes" is if any of the 5 columns has "Yes" and "No" is if none of the columns were ever selected

I was able to sum them to figure out how many should be in each group, but I am unsure how to create the new variable

data$pyes <- sum(data$pocket_biopsy== "Yes" | data$pxray == "Yes" | 
      data$pxray == "Yes" |
      data$pchemo == "Yes" | 
      data$pconsult == "Yes", na.rm=TRUE)

data$pno <-sum(data$pbiopsy== "No" & data$pxray == "No" & 
      data$pxray == "No" &
      data$pchemo == "No" & 
      data$pconsult == "No", na.rm=TRUE)
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 27 '23 at 22:02
  • Perhaps something like `rowSums(data[,c("pocket_biopsy","pxray","pchemo","pconsult")] %in% "Yes")` tells you how many `"Yes"`es there are per row (not counting `NA` values, the reason I use `%in%` instead of `==`). Does that give you enough to start? – r2evans Jan 27 '23 at 22:14
  • `sum(data$pocket_biopsy== "Yes"` and `sum(data$pbiopsy== "No"` implies you are testing 2 different columns. Is that correct or a typing error? – SteveM Jan 28 '23 at 00:14

1 Answers1

0

You've got most of the way there.

(data$pocket_biopsy== "Yes")|(data$pxray == "Yes")|(data$pxray == "Yes")|(data$pchemo == "Yes")|(data$pconsult == "Yes")

will get you a vector of trues and falses.

To turn that into "Yes" and "No" you just need to feed that into "ifelse"

ifelse( (data$pocket_biopsy== "Yes")|(data$pxray == "Yes")|(data$pxray == "Yes")|(data$pchemo == "Yes")|(data$pconsult == "Yes") ,  "Yes", "No")

Replace the |s with &s for AND.

Fhnuzoag
  • 3,810
  • 2
  • 20
  • 16