Creating an index using survey data

Hi,

I am new to R and want to create an index based on answers to 5 survey questions.

I want to then use the index as the dependent variable in some regressions later on.

Are these functions compatible in r or should i try something else?

Thanks :slight_smile:

You can do all those things in R (and much more) for sure, but if you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thanks andresrcs.

I am trying to recode answers to the survey for the index and I keep getting:

Error: Can't convert < character> to < double >.
Run rlang::last_error() to see where the error occurred.

The data i am trying to recode is

Labels:
value label
1 Very good
2 Good
3 So so (not good nor bad)
4 Bad
5 Very bad
7 Do not understand the question
8 Can't choose
9 Decline to answer

and the code i have tried to use is:
w3q1.1 <- recode(ABS3.1$q1, "1='5. Very good'; 2='4. Good'; 3='3. So so (not good or bad)'; 4='2. Bad'; 5='1. Very bad'; 7='7. Do not understand the question'; 8='8.Can't choose'; 9='9. Decline to answer'")

When i run:

rlang::last_error()

The console says this:
'Backtrace:

  1. car::recode(ABS3.1$q1, "1='5. Very good'; 2='4. Good'; 3='3. So so (not good or bad)'; 4='2. Bad'; 5='1. Very bad'; 7='7. Do not understand the question'; 8='8.Can't choose'; 9='9. Decline to answer'")
  2. vctrs:::[<-.vctrs_vctr(*tmp*, var == val, value = "9. Decline to answer")
  3. vctrs::vec_cast(value, x)
  4. haven:::vec_cast.haven_labelled.character(...)
  5. haven:::vec_cast.haven_labelled.haven_labelled(x, to, ...)
  6. vctrs::vec_cast(...)
  7. vctrs::vec_default_cast(...)
  8. vctrs::stop_incompatible_cast(...)
  9. vctrs::stop_incompatible_type(...)
  10. vctrs:::stop_incompatible(...)
  11. vctrs:::stop_vctrs(...)'

Any advice?

Do you have semicolons where there should be commas? Also, I think quotes are in the wrong place.

Removed the semicolons and it says:
Error in recode(ABS3.1$q1, "1='5. Very good', 2='4. Good', 3='3. So so (not good or bad)', 4='2. Bad', 5='1. Very bad'") :

in recode term: 1='5. Very good', 2='4. Good', 3='3. So so (not good or bad)', 4='2. Bad', 5='1. Very bad'
message: Error in parse(text = strsplit(term, "=")[[1]][2]) :
:1:15: unexpected ','
1: '5. Very good',
^

where should the quotation marks go?

recode() is kind of confusing. It will probably be replaced one day. In the meantime, you don't want the double quotes. And the syntax is old value = new value, unlike most other functions.

You may want to look at the help for recode()

?recode

Thanks for the tip.

I think i solved the issue.

Had to change from character vector to a numeric.

Used this video if anyone else wants to know:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.