I have a data with several variables and I want to use a for to scroll through several variables and check the values.
The data table data contains the variables V1, V2 ... V15. What I want is to create a Stotal variable that is the sum of the SI values that exist in the variables V1 to V15.
for (i in 1:nrow(datos)){
for (j in 1:15){
(if(sprintf("V%d",j)[i])=="SI"){
aux<-1
Stotal[i]<-1+aux
}
}
The code does not work, among other things, because the sprintf function pastes the text but does not take the value of the variable in the position that I ask.
Edited: An example with fewer columns could be:
V1 <- c("Si","No","Si")
V2 <- c("Si","No","No")
V3<-c("Si","Si","Si")
datos <- data.frame(V1,V2,V3)
datos
V1 V2 V3
1 Si Si Si
2 No No Si
3 Si No Si
In this case the expected output would be: 3 1 2