0

I'm trying to see if any person endorsed the same response option for each item on a scale. If the person did do that, I want to create a new variable in the dataset that has a 1 in their row, and a 0 if they didn't. There are 4 rows of NAs in the columns I'm using, and I don't want to delete the rows.

I've tried ifelse(rowVars(dro[cols]) == 0, 1, 0) but the 4 NAs get removed, so I can't place the result back into the original dataset. I've tried rowMins and rowMaxs, which end up somehow shifting the NAs 4 rows up. I've also tried apply() which does the same thing as rowMins.

If anyone has any advice on how to proceed, I would be very appreciative.

r2evans
  • 141,215
  • 6
  • 77
  • 149
acc
  • 1
  • 4
    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 Mar 30 '20 at 22:10
  • Please be a little more attentive to tags used: using unrelated tags is a form of what I call "tag-fishing", similar to how spam websites put in random keywords trying to encourage search engines to reference them. Specifically, [tag:min], [tag:max], and [tag:variance] do not (afaict) relate to matching values. I know that the Stack question-asking system will come up with several recommendations and encouraging more than one, but it helps when the tags used are *meaningful*. – r2evans Mar 30 '20 at 22:23
  • This is the code I ran: dro$iddc_match <- ifelse((apply(dro[cols], 1, !is.na(dro[cols]), FUN=min)) == (apply(dro[cols], 1, !is.na(dro[cols]), FUN=max )) , 1, 0) – acc Mar 30 '20 at 22:26
  • How do you identify `if any person endorsed the same response option for each item on a scale.`? If you add data using `dput(head(dro))` in your post it would be very helpful. – Ronak Shah Mar 31 '20 at 04:00
  • acc, comments are bad for most code and data; even if the formatting works (it does poorly here), comments are easy to skip by readers, and the interface may hide them when sufficient comments are present. Please [edit] your question and add relevant code there (and please read https://stackoverflow.com/editing-help). – r2evans Mar 31 '20 at 07:24

0 Answers0