This was very helpful, thank you
Hi!
To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
More specifically for you, it would be sufficient to run your workflow up until the point where you would have run the code you pasted here, i.e. when you have gss1000 but havent adjusted it yet, and provide a 20record sample. Therefore the code you would use to provide this to the forum would be
dput(head(gss1000,n=20))
Then, format your forum posts by using triple backticks like so :
```
my code looks nice
Hello,
Is this what you mean?
dput(head(gss1000$vote,n=20))
c(NA, 1, NA, NA, 1, 1, NA, 3, 3, 6, NA, 5, NA, 4, 2, 1, 1, NA,
3, 4)
I would have preferred the code as I wrote it without the $vote part, but its acceptable...
It's a massive data set with 5000 values
ok, thanks, but this one doesnt have a vote field at all, not sure what happened for you....anyway,
Try using case when to assign the values.
(gss1000<-data.frame(
vote=c(NA, 1, NA, NA, 1, 1, NA, 3, 3, 6, NA, 5, NA, 4, 2, 1, 1, NA,
3, 4),
some_other_columns=1:20))
#
# if it is missing or NA, I wan to code as 1 and assign the label "unknown",
# if the value is 1, I want to assign value of 2 with label "never",
# if between 2-4, I want to assign value of 3 with label "minimal",
# and if between 5-7, I want to assign value of 4 with label "average"
library(tidyverse)
(gss1000 <- mutate(gss1000,
new_vote = factor(case_when(vote==1~2,
between(vote,2,4)~3,
between(vote,5,7)~4,
TRUE ~ 1),
labels = c('unkown',
'never',
'minimal',
'average'))))
table(gss1000$new_vote)
# unkown never minimal average
# 7 5 6 2