Finalsenator
Finalsenator

Reputation: 1

Converting text to numbers?

So I am trying to code gender as a dummy variable. However, the data has gender coded as "1.Male" and "2.Female" (rather than 1=Male and 2=Female) I just want to recode it so it comes out as 1's and 2's

df$men <- car::recode(df$gender_respondent_x, "1=1; 2=0; else=NA")
table(df$men, df$gender_respondent_x, useNA = "ifany")

#       1. Male 2. Female
#  <NA>    2845      3069

Upvotes: 0

Views: 50

Answers (1)

koki25ando
koki25ando

Reputation: 74

I think extract_numeric() from tidyr package is very useful.

library(tidyr)
table(extract_numeric(df$gender_respondent_x))

This function literally extract only digits from strings. I hope you find this helpful. Check out this official document for details.

Upvotes: 0

Related Questions