0

I am new to R and using it for my studies in PSychology.

Right now, I am analyzing a survey with 5 open text fields at the end. What interests me, is the amount of filled out text fields. That is why I need to create a new variable ('behave') that contains the information of how many of these text fields were filled out by each participant.

#right now the df looks similar to this (t= text field filled out): 

S1 <- c(T,T,NA,T)
S2 <- c(T,NA,NA,T)
S3 <- c(T,NA,NA,NA)
S4 <- c(T,NA,NA,NA)
S5 <- c(NA,NA,NA,NA)


#and this is what I want it to look like: 

S1 <- c(T,T,NA,T)
S2 <- c(T,NA,NA,T)
S3 <- c(T,NA,NA,NA)
S4 <- c(T,NA,NA,NA)
S5 <- c(NA,NA,NA,NA)
behave<- c(4, 1, 0, 2)

I tried to use ifelse to somehow work my way to this variable but this seems very complicated and prone to mistakes. That is why I wanted to ask whether there was a more simple way to do that.

justus
  • 1

1 Answers1

0
S1 <- c(T,T,NA,T)
S2 <- c(T,NA,NA,T)
S3 <- c(T,NA,NA,NA)
S4 <- c(T,NA,NA,NA)
S5 <- c(NA,NA,NA,NA)

res<-data.frame(S1,S2,S3,S4,S5)

apply(res,1,function(x) sum(!is.na(x)))

4 1 0 2