samedi 11 août 2018

Conditional if/else statement in R

I am learning to improve my coding in R. I have this code:

data$score[testA == 1] <- testA_score
data$score[testB==1] <- testB_score

So basically I have four colummns that I want to combine into one: testA=1 indicates if the student took version A of the test and testA_score is their score; testB=1 indicates if the student took version B of the test and testB_score is their score. I want to combine this information into new column score.

As well Suppose I had testA, testB through testH. All values are 0 or 1. How can I make new column test_complete which is = 1 if any of the tests are = 1?

Basically as a former Stata user I am looking for the R equivalent commands to egen rowtotal and egenrowfirst. Thanks so much.

Aucun commentaire:

Enregistrer un commentaire