-2

I am rather new to R and am struggling to recode/rename the variable answer groupings. The values within the groups are correct, it's just that the names of them are not.

I changed the variable from 'factor' to 'numeric'

The dataset is called 'nes', the variable is 'age' These are the different methods I have tried...

with(age, numeric(Answer, 
    levels = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13'), labels = c("17-20", "21-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74", "75+" )))

recode(age, 1 = '17-20', 2 = '21-24', 3 = '25-29' ,4 = '30-34', 5 = '35-39', 6 = '40-44', 7 = '45-49', 8 = '50-54', 9 = '55-59', 10 = '60-64', 11 = '65-69', 12 = '70-74', 13 = '75+')
num_vec <- age(1:13, NA)
recode(num_vec, '1' = 17-20, '2' = 21-24, '3' = 25-29 ,'4' = 30-34, '5' = 35-39, '6' = 40-44, '7' = 45-49, '8' = 50-54, '9' = 55-59, '10' = 60-64, '11' = 65-69, '12' = 70-74, '13' = 75+)

colnames(nes) <- age("17-20", "21-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74, "75 and above")

nes %>% mutate(age=recode(age, 
                         '1'= "17-20",
                         '2'= "21-24",
                         '3'= "25-29",
                         '4'= "30-34",))

 setNames(age('1', '2', '3'), age('17 to 20', '21 to 24', '25 to 9'))nes$age]

everything I do is coming back with these errors:

> mutate(Answer = recode(Answer, 1='17-20', 2='21-24', 3='25-29' ,4 ='30-34', 5='35-39', 6='40-44', 7='45-49', 8='50-54', 9='55-59', 10='60-64', 11='65-69', 12='70-74', 13='75+')
Error: unexpected '=' in "mutate(Answer = recode(Answer, 1="
> mutate(age = recode(age, 1='17-20', 2='21-24', 3='25-29' ,4 ='30-34', 5='35-39', 6='40-44', 7='45-49', 8='50-54', 9='55-59', 10='60-64', 11='65-69', 12='70-74', 13='75+')
Error: unexpected '=' in "mutate(age = recode(age, 1="
> mutate(age = recode(age, 1= '17-20', 2= '21-24', 3= '25-29' ,4 = '30-34', 5= '35-39', 6= '40-44', 7= '45-49', 8= '50-54', 9= '55-59', 10= '60-64', 11= '65-69', 12= '70-74', 13= '75+')
Error: unexpected '=' in "mutate(age = recode(age, 1="
> with(age, factor(Answer, 
+     levels = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13'), labels = c("17-20", "21-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74", "75+" )))
Error in eval(substitute(expr), data, enclos = parent.frame()) : 
  numeric 'envir' arg not of length one
> with(age, numeric(Answer, 
+     levels = c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13'), labels = c("17-20", "21-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74", "75+" )))
Error in eval(substitute(expr), data, enclos = parent.frame()) : 
  numeric 'envir' arg not of length one

> recode(age, 1= '17-20', 2= '21-24', 3= '25-29' ,4 = '30-34', 5= '35-39', 6= '40-44', 7= '45-49', 8= '50-54', 9= '55-59', 10= '60-64', 11= '65-69', 12= '70-74', 13= '75+')
Error: unexpected '=' in "recode(age, 1="

> recode(age, 1 = '17-20', 2 = '21-24', 3 = '25-29' ,4 = '30-34', 5 = '35-39', 6 = '40-44', 7 = '45-49', 8 = '50-54', 9 = '55-59', 10 = '60-64', 11 = '65-69', 12 = '70-74', 13 = '75+')
Error: unexpected '=' in "recode(age, 1 ="

> recode(num_vec, '1' = 17-20, '2' = 21-24, '3' = 25-29 ,'4' = 30-34, '5' = 35-39, '6' = 40-44, '7' = 45-49, '8' = 50-54, '9' = 55-59, '10' = 60-64, '11' = 65-69, '12' = 70-74, '13' = 75+)

Error: unexpected ')' in "recode(num_vec, '1' = 17-20, '2' = 21-24, '3' = 25-29 ,'4' = 30-34, '5' = 35-39, '6' = 40-44, '7' = 45-49, '8' = 50-54, '9' = 55-59, '10' = 60-64, '11' = 65-69, '12' = 70-74, '13' = 75+)"

> colnames(age) <- c("17-20", "21-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74, "75+", )
Error: unexpected input in "colnames(age) <"

> colnames(nes) <- age("17-20", "21-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74, "75+", )
Error: unexpected input in "colnames(nes) <"
> nes %>% mutate(age=recode(age, 
+                          `1`= "17-20",
+                          `2`="21-24"
+                          '3'= "25-29"
Error: unexpected string constant in:
"                         `2`="21-24"
                         '3'"
> nes %>% mutate(age=recode(age, 
+                          '1'= "17-20",
+                          '2'= "21-24"
+                          '3'= "25-29"
Error: unexpected string constant in:
"                         '2'= "21-24"
                         '3'"
> nes %>% mutate(age=recode(age, 
+                          '1'= "17-20",
+                          '2'= "21-24",
+                          '3'= "25-29",
+                          '4'= "30-34",))
Error in nes %>% mutate(age = recode(age, `1` = "17-20", `2` = "21-24",  : 
  could not find function "%>%"
>  setNames(age('1', '2', '3'), age('17-20', '21-24', '25-29'))nes$age]
Error: unexpected symbol in " setNames(age('1', '2', '3'), age('17-20', '21-24', '25-29'))nes"
>  setNames(age('1', '2', '3'), age('17 to 20', '21 to 24', '25 to 9'))nes$age]
Error: unexpected symbol in " setNames(age('1', '2', '3'), age('17 to 20', '21 to 24', '25 to 9'))nes"
> class(age)
[1] "numeric"
> 
> age<-as.factor(age)
> setNames(age('1', '2', '3'), age('17 to 20', '21 to 24', '25 to 9'))nes$age]
Error: unexpected symbol in "setNames(age('1', '2', '3'), age('17 to 20', '21 to 24', '25 to 9'))nes"
> colnames(nes) <- age("17-20", "21-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74, "75 and above",)
Error: unexpected input in "colnames(nes) <"
> recode(num_vec, '1' = 17-20, '2' = 21-24, '3' = 25-29 ,'4' = 30-34, '5' = 35-39, '6' = 40-44, '7' = 45-49, '8' = 50-54, '9' = 55-59, '10' = 60-64, '11' = 65-69, '12' = 70-74, '13' = 75+)
Error: unexpected ')' in "recode(num_vec, '1' = 17-20, '2' = 21-24, '3' = 25-29 ,'4' = 30-34, '5' = 35-39, '6' = 40-44, '7' = 45-49, '8' = 50-54, '9' = 55-59, '10' = 60-64, '11' = 65-69, '12' = 70-74, '13' = 75+)"

what am I doing wrong??

user438383
  • 5,716
  • 8
  • 28
  • 43
cl1791
  • 1
  • 3
    Please provide a working example of your data (I just tried to copy the first line and was given an error). Also, try to reduce some text. It makes it hard to focus on your issue. Good luck helping us helping you! :) – RYann Dec 11 '22 at 21:19

1 Answers1

1

Although you should definetly fix your question (no need to put examples of wht didnt works), I think i understood what you tried....

library(tidyverse)
nes <- data.frame(
  age = factor(sample(13,100,T))
)

nes$age_correct <- fct_recode(nes$age,"17-20" = "1",
           "21-24"="2","25-29"="3",
           "30-34"="4","35-39"="5",
           '40-44'="6",'45-49'='7',
           '50-54'='8','55-59'='9',
           '60-64'='10','65-69'='11',
           '70-74'='12',"75+"='13')
RYann
  • 567
  • 1
  • 7