0

I'm trying to create new dummy variable ps2female (0=male, 1=female) by using information from existing variable bysex (1=male, 2=female), but keep gets an error.

#Loading Data (data to R)
library(tidyverse)
library(haven)
els02 <- read_dta(file="els02_processed_small.dta")
els02$bysex
value     label
<fctr>    <fctr>
-8  Survey component legitimate skip/NA         
-4  Nonrespondent           
 1  Male            
 2  Female          

 4 rows
library(dplyr)
els02 %>%
  mutate(ps2female = recode(bysex, "1" = "0", "2" = "1"))

Error in recode(bysex, `1` = "0", `2` = "1") : unused arguments (`1` = "0", `2` = "1")

Any suggestions? Thanks in advance.

massisenergy
  • 1,764
  • 3
  • 14
  • 25
Jassacsd
  • 1
  • 1
  • Can you use `dput(head(els02))` to directly share a sample of data to see structure? Also may consider `dplyr::recode` in case of masked function. – Ben Feb 23 '20 at 16:58
  • You appear to be recoding a factor, looking at the documentation it adds `.default = levels(factor_vec))` to the recode. Did you try something like that? – Annet Feb 23 '20 at 17:14
  • 1
    That looks a lot like a masked function name. What libraries have you loaded?And as @Ben has already mentioned: Please add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) that way you can help others to help you! – dario Feb 23 '20 at 17:16
  • Don't use backticks, use standard single quotes or double quotes. This works: `dplyr::recode(1:2, '1' = '0', '2' = '1')`. – Rui Barradas Feb 23 '20 at 17:42

0 Answers0