I am quite a beginner in R and I am stuck with this problem:
I have a dataset (data) of 24 columns. Each column is an answer to a question : "Is your xxx hurting"? xxx varies in each column (it can be back, head...) Each column can take 3 values: 1 if it does not hurt, 2 if it hurts a bit, 3 if it pains.
I want to create a new variable (fragile) to count each time a person said it hurted a bit somewhere.
data$fragile <- ifelse(data$var1 ==2 , 1, 0) + ifelse(data$var2 == 2, 1, 0) + ...
But it is quite long so I would like to not do it
I tried doing a loop but I read somewhere that R is not made to do loops (and my loop does not work anyway)
data$fragile <- for(i in dataset[1:24]){ifelse(i == 2, 1, 0)}
And when I do head(data$fragile)
, "NULL" appears...
What should I do?
I am sure this question has already been asked several times but I do not find the right keywords.