0

Below is my R code, but I couldn't decipher the "result" part. It doesn't work the way I want (see the Excel image below).

library(tidyverse)
    
Question_No <-c(1,1,1,1,1,10,10,10,10,10,11,11,11,11,11)

Reply<-c("never","few","medium","much","complete",
         "never","few","medium","much","complete",
         "never","few","medium","much","complete")

Number_of_Answers<- c(7,1,12,13,18,6,2,12,10,21,6,2,13,11,19)

aa<- data.frame(Question_No,Reply,Number_of_Answers)

replace<- function(x)
{Replys=x['Reply'] 
if(Replys=="few"){return(2)}
else if(Replys=="much"){return(4)} 
else if(Replys=="never"){return(1)}
else if(Replys=="medium"){return(3)}
else {return(5)}}

Replys<- apply(aa,MARGIN = 1,FUN = replace)

aa['Replys']<-Replys


aa= mutate(aa,NA_R = aa$Replys * aa$Number_of_Answers)

aa$result = ifelse(aa$Question_No==1, sum(aa$NA_R) ,"n")

View(aa)

My aim is to use R to reproduce the result in EXCEL shown in the picture below.

Thank you for the help.

Image of Excel output I'm trying to reproduce

  • 2
    It is not clear what you are trying to do – GuedesBF Jun 08 '21 at 14:43
  • You may want to enclose the variable name in backticks: `ifelse(aa$\`Question No\`==1, sum(aa$RN)` – GuedesBF Jun 08 '21 at 14:44
  • 3
    [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with. We don't have access to your data, so we can't run your code, and can only guess as to what exactly the issue is. You also have syntax errors—you can't have spaces in your column names – camille Jun 08 '21 at 15:53

0 Answers0