1

I am trying to recode the variable FamilyStatus in my dataframe na.df. The characters ("ledig", "verheiratet" and "sonstiges" should be recoded in numerics. I know, there are many similar questions and answers already but nothing fixed my problem, so I ask here. The same code structure works with other dataframes of mine. But what is wrong here?

this is my sample data:


head(na.df)
# A tibble: 6 x 5
  StudyID   Age FamilyStatus EducationalLevel         CycleLength
    <dbl> <dbl> <chr>        <chr>                          <dbl>
1    1016    23 ledig        Gymnasium                         28
2    1007    28 ledig        Gymnasium                         30
3    1014    23 ledig        Gymnasium                         28
4    1006    21 ledig        Gymnasium                         28
5    1050    41 ledig        Universität / Hochschule          27
6    1001    26 ledig        Gymnasium                          4


na.df.1 <- mutate(na.df, 
                  FamilyStatus = recode(FamilyStatus, 
                                        "ledig"= '1',
                                        "verheiratet" = '2', 
                                        "sonstiges" = '3'),
                  EducationalLevel = recode(EducationalLevel,
                                          "Sekundar- / Realschule"= '1',
                                          "Gymnasium" = '2',
                                          "Universität / Hochschule" = '3'))


Fehler: Problem with `mutate()` input `FamilyStatus`.
x unbenutzte Argumente (ledig = "1", verheiratet = "2", sonstiges = "3")
ℹ Input `FamilyStatus` is `recode(FamilyStatus, ledig = "1", verheiratet = "2", sonstiges = "3")`.
Run `rlang::last_error()` to see where the error occurred.

Thanks for your help!

cello
  • 11
  • 2
  • Just to clarify, you want to recode as "1" instead of 1? – latlio Jan 13 '21 at 17:45
  • I need a numeric vector for further tests -> so I want torecode 1 instead of ledig etc and for the second variable 1 instead of Sekundar-/ Realschule. – cello Jan 13 '21 at 17:59
  • That's what I thought. I don't think you want "ledig" = '1'... I think you want "ledig" = 1, and same goes for the other variables – latlio Jan 13 '21 at 18:21
  • Please provide a sample data of `na.df` so that we can see what you are trying to do. – Phil Jan 13 '21 at 18:21
  • ok i deletet the ' ' at the 1 but it still gives the same error – cello Jan 13 '21 at 18:29
  • Please try `dplyr::recode`, it looks like a namespace conflict to me. – TimTeaFan Jan 13 '21 at 19:02

2 Answers2

0

Does this still give the same error message for "...input FamilyStatus"?

na.df1 <- na.df %>% 
  mutate(FamilyStatus = recode(FamilyStatus, 
                               "ledig" = 1,
                               "verheiratet" = 2,
                               "sonstiges" = 3
                               ))
latlio
  • 1,567
  • 7
  • 15
0
dplyr::recode(na.df$FamilyStatus, ledig= "1", verheiratet="2", sonstiges="3")

this worked!!

EDIT

This seems to have been a name spacing issue.

Jan
  • 4,974
  • 3
  • 26
  • 43
cello
  • 11
  • 2