0

I will create survey with r programming. But i am new one. I want to count "yes" and "no" answer which will ask each people. And create data with them. How can i do this? My code is as follows:

 survey <- c ("Are you student?", "Do you drive car?", "Do you smoke cigarette?")

surveyfunct <- function(v,m){
   for(i in 1:length(v))
       {m <-readline(v[i])
        if (m == "yes" && m == "no"){
            m <- readline(v[i+1])}}
    return (list(m))}
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
davut
  • 41
  • 1
  • 7
  • Please can you show your full code to make your question reproducible. Remember that R is vectorised so code like sum(v == "yes") are useful – Richard Telford Apr 12 '16 at 07:46
  • This is also my full code and this is my full question which is trying. "Create a survey which contains 20 (Yes/No) questions. Questions will be seen by user respectively and should accept only “Yes” or “No” as the answer. You should create a vector for keeping all answers that user give. Therefore, after one user finishes answering all questions save his/her answers into a file with name “answers.csv”, if a new user fills the survey his/her answers should be appended into the same file." – davut Apr 12 '16 at 07:56
  • Do you want to use R as a stimulus presentation env? 1. It's not advisable for many reasons (I would go for psychopy instead) 2. In case you missed it, please have a look at [link](http://stackoverflow.com/questions/11007178/creating-a-prompt-answer-system-to-input-data-into-r)? 3. If really have to/want to do this in R, have a look at Shiny [link](http://shiny.rstudio.com/) – Pasqui Apr 12 '16 at 08:09

1 Answers1

1

For collect answers you can use such fucntion

survey <- c ("Are you student?", "Do you drive car?", "Do you smoke cigarette?")

surveyfunct <- function(q){
  
  return (readline(q))}

answers=sapply(survey,surveyfunct)

then you can subset needed answer or write all of them into csv.

if your want only yes\no answer you can add while

surveyfunct <- function(q){
  h=0
  while (h==0){
    
    a=readline(q)
    if( a %in% c("yes","no")){
      h=1}
  }
  return (a)}

For count you can simply use table

example

> answers=sapply(survey,surveyfunct)
Are you student?yes
Do you drive car?no
Do you smoke cigarette?yes
> table(answers)
answers
 no yes 
  1   2 
Community
  • 1
  • 1
Batanichek
  • 7,761
  • 31
  • 49