I have a variable that is currently "dbl" and values have been coded 1-10. However, I need it to be integer and renaming the value to its appropriate name. For example; value 1 = 5, value 2 = 6, values 3 = 7...... the ggplot scale should plot values 5-14 not 1-10. I am not sure how to do this and cannot find a correct code to convert to integer and change the values? Is this even possible?
(df_ <- data.frame(nums = 5:14 * 1.0000001))
df_$ints <- as.integer(df_$nums)-4L
#take a look
str(df_)
Thank you for responding but I am sorry I don't understand the code (df_ ?
My dataframe is called socialactiondata and the variable I want changed is 'year of school' - I tried the code below?
(socialactiondata_ <- socialactiondata(nums = 5:14 * 1.0000001))
df_$ints <- as.integer(df_$nums)-4L
But error is
Error in socialactiondata(nums = 5:14 * 1.0000001) :
could not find function "socialactiondata"
I think you need
socialactiondata$`year of school` <- as.integer(socialactiondata$`year of school`)-4L
Hello, that is not working either - error message
Warning: Unknown or uninitialised column: `year of school`.Error:
! Assigned data `as.integer(socialactiondata$\`year of school\`) - 4L` must be compatible with existing data.
✖ Existing data has 1100 rows.
✖ Assigned data has 0 rows.
ℹ Only vectors of size 1 are recycled.
Backtrace:
1. base::`$<-`(`*tmp*`, `year of school`, value = `<int>`)
12. tibble (local) `<fn>`(`<vctrs___>`)
you told us you had a variable `year of school`
in socialactiondata
data.frame
but I suppose your error message is evidence that you dont ?
prehaps look at the names with
names(socialactiondata)
Thanks for your help but here is what I get when I do code you suggest name(socialactiondata):
[1] "Year of School" "Frequency of Activity" "Type of Area"
[4] "Nation" "Social Class" "Age Group"
[7] "Sex" "Ethnicity" "Age of First Action Activity"
This shows I do have a variable named 'year of school' which is double with values 1-10 that I need to change to 5-14 but no codes I am using are working
R is case sensitive so “word” is not the same as “Word”
I understand - I used tried code with lower case and upper case and still have not got result I need.
The code below has changed variable to integer ....
socialactiondata$Year of School
<- as.integer(socialactiondata$Year of School
)-4L
Rows: 1,100
Columns: 9
$ Year of School
2, 2, 0, 0, 3, 3, 1, 1, -2, -2, 0, 0, -2, -2, -3, -3, 1, 1,…
but the values are still not correct, there should not be any -2 or any negative number
The values 1-10 should be 5-14 because the codebook says
1 = 5
2= 6
3=7
4=8
5=9
6=10
7=11
8=12
9=13
10=14
I understand. The code below changes the variable to integer
socialactiondata$Year of School
<- as.integer(socialactiondata$Year of School
)-4L
I get this result
Rows: 1,100
Columns: 9
$ Year of School
2, 2, 0, 0, 3, 3, 1, 1, -2, -2, 0, 0, -2, -2, -3, -3, 1, 1,…
However, this is still not correct. There should not be any negative numbers as the code book from the dataset tells me the following:
Value 1 = 5 (year 5 of school)
2=6
3=7
4=8
5=9
6=10
7=11
8=12
9=13
10=14
If Year of School = 1 means year 5 of school, then why are you subtracting 4 instead of adding it?
It would be helpful if you posted the results of head(socialactiondata).
This topic was automatically closed 21 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.