1
votes

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!

2
Just to clarify, you want to recode as "1" instead of 1? - latlio
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
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
Please provide a sample data of na.df so that we can see what you are trying to do. - Phil
ok i deletet the ' ' at the 1 but it still gives the same error - cello

2 Answers

0
votes

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
                               ))
0
votes
dplyr::recode(na.df$FamilyStatus, ledig= "1", verheiratet="2", sonstiges="3")

this worked!!

EDIT

This seems to have been a name spacing issue.