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??