[R Survey Analysis] Problem counting number of responses

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[R Survey Analysis] Problem counting number of responses

Lauren Bolger
M
​y dataset includes a survey completed by research participants to evaluate
quality of life. A few things regarding the survey:


   -
*not all questions must be answered​ for the total score *
   - questions left blank are coded as "0"
   - ​the number of questions answered must be determined in order to
   compute the total score


*Sample Code​*

> q1 <- ifelse(is.na(survey, 0, survey$q1)

> q2 <- ifelse(is.na(survey, 0, survey$q2)

> q3 <- ifelse(is.na(survey, 0, survey$q3)

> survey$sum.survey <- q1 + q2 + q3

> survey$responses <- # HELP ?????
*> # HELP PART 1 ------------------------------*
*> # need help determining number of questions the respondents answered*

> *# HELP PART 2 -------------------------------*
*> # I want to look at the subset of the population that completed all the
questions*




​Thanks for your help!! It means a lot​ :)

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [R Survey Analysis] Problem counting number of responses

glsnow
Lauren,

The easier that you make it for us to help you, the more likely you
are to get help and the more helpful the answers will be.

First, please post in plain text (not HTML).  Second, reproducible
code (including sample data) helps us help you.

Your code above is incorrect, the 3 lines with "ifelse" have more left
parentheses than right parentheses.

Is "survey" a data frame? or something else?

Best guess from your code is that you want something like:

survey$responses <- with(survey, !is.na(q1) + !is.na(q2) + !is.na(q3))

the "with" function just makes it so you don't have to prepend
everything with "survey$", the "!" inverts logic so that it shows
those that are not NA and when you sum logicals they are converted to
0/1.

With 3 questions then you can subset your data like:

survey[ survey$nesponses==3, ]

to see those that answered (or did not result in NA) 3 questions.



On Tue, Aug 16, 2016 at 12:51 PM, Lauren Bolger <[hidden email]> wrote:

> M
> y dataset includes a survey completed by research participants to evaluate
> quality of life. A few things regarding the survey:
>
>
>    -
> *not all questions must be answered for the total score *
>    - questions left blank are coded as "0"
>    - the number of questions answered must be determined in order to
>    compute the total score
>
>
> *Sample Code*
>
>> q1 <- ifelse(is.na(survey, 0, survey$q1)
>
>> q2 <- ifelse(is.na(survey, 0, survey$q2)
>
>> q3 <- ifelse(is.na(survey, 0, survey$q3)
>
>> survey$sum.survey <- q1 + q2 + q3
>
>> survey$responses <- # HELP ?????
> *> # HELP PART 1 ------------------------------*
> *> # need help determining number of questions the respondents answered*
>
>> *# HELP PART 2 -------------------------------*
> *> # I want to look at the subset of the population that completed all the
> questions*
>
>
>
>
> Thanks for your help!! It means a lot :)
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [R Survey Analysis] Problem counting number of responses

Sarah Goslee
In reply to this post by Lauren Bolger
Hi Lauren,

I'm not entirely sure what your sample code is suppoesd to do, since
it isn't complete R code, and it would be much easier to answer your
question if you provided sample data and didn't post in HTML.

dput(head(survey))

would be enough sample data, most likely.

But if I'm understanding correctly,
nanswered <- rowSums(survey != 0)
will give you for each respondent the number of questions answered.

or if they're actually NA instead of 0
nanswered <- rowSums(!is.na(survey))

then you can look at the distribution of those values, and use that
vector with subset() to get the population that answered all of your
questions.

The advantage of this approach is that you don't have to list each
question individually, in case you have lots.

Sarah

On Tue, Aug 16, 2016 at 2:51 PM, Lauren Bolger <[hidden email]> wrote:

> M
> y dataset includes a survey completed by research participants to evaluate
> quality of life. A few things regarding the survey:
>
>
>    -
> *not all questions must be answered for the total score *
>    - questions left blank are coded as "0"
>    - the number of questions answered must be determined in order to
>    compute the total score
>
>
> *Sample Code*
>
>> q1 <- ifelse(is.na(survey, 0, survey$q1)
>
>> q2 <- ifelse(is.na(survey, 0, survey$q2)
>
>> q3 <- ifelse(is.na(survey, 0, survey$q3)
>
>> survey$sum.survey <- q1 + q2 + q3
>
>> survey$responses <- # HELP ?????
> *> # HELP PART 1 ------------------------------*
> *> # need help determining number of questions the respondents answered*
>
>> *# HELP PART 2 -------------------------------*
> *> # I want to look at the subset of the population that completed all the
> questions*
>
>
>
>
> Thanks for your help!! It means a lot :)
>
>         [[alternative HTML version deleted]]
>
--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [R Survey Analysis] Problem counting number of responses

Jim Lemon-4
In reply to this post by Lauren Bolger
Hi Lauren,
As Sarah noted, if your blank responses are coming as NAs, it may be
best to leave them alone until you have done the calculations:

survey$responses<-!is.na(survey[,c("q1","q2","q3")])
survey$sum_survey<-rowSums(survey[,c("q1","q2","q3")],na.rm=TRUE)
# the next line returns a logical vector that you can use to subset
# like this survey[q_complete,]
q_complete<-complete.cases(survey[,c("q1","q2","q3")])

then set your NAs to zeros if you need to.

Jim


On Wed, Aug 17, 2016 at 4:51 AM, Lauren Bolger <[hidden email]> wrote:

> M
> y dataset includes a survey completed by research participants to evaluate
> quality of life. A few things regarding the survey:
>
>
>    -
> *not all questions must be answered for the total score *
>    - questions left blank are coded as "0"
>    - the number of questions answered must be determined in order to
>    compute the total score
>
>
> *Sample Code*
>
>> q1 <- ifelse(is.na(survey, 0, survey$q1)
>
>> q2 <- ifelse(is.na(survey, 0, survey$q2)
>
>> q3 <- ifelse(is.na(survey, 0, survey$q3)
>
>> survey$sum.survey <- q1 + q2 + q3
>
>> survey$responses <- # HELP ?????
> *> # HELP PART 1 ------------------------------*
> *> # need help determining number of questions the respondents answered*
>
>> *# HELP PART 2 -------------------------------*
> *> # I want to look at the subset of the population that completed all the
> questions*
>
>
>
>
> Thanks for your help!! It means a lot :)
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.