0

I'm doing an economics replication paper, and I need to estimate the year that an immigrant entered the labour market.

This expression is supposed to tell me if I can use their arrival to America as the year of entry into the labour market or not, since some people stay in school (df_clean$ysm < df_clean$AGE - df_clean$EDUCD - 5)

For those that have been in the US longer than the time they spent in school in addition to five extra years for being a toddler/baby, I can use the date of immigration (df_clean$YRIMMIG).

For those that haven't been here as long, their year of entry into the labour market in the US will need to account for the time they've spent in school before entering the labour market in America (df_clean$YRIMMIG + df_clean$EDUCD + 5).

How can I make a new variable that can estimate when they joined the US labour force?

library(dplyr)
class(df_clean$YRIMMIG) #integer # date/year of immigration to US
class(df_clean$ysm) # numeric # quantity of years since the migration
class(df_clean$AGE) #integer # age in years
class(df_clean$EDUCD) #numeric #number of years educated in America
class(df_clean$year_entry) # integer # year the immigrant entered into the labour force

df_clean$YRIMMIG <- as.numeric(df_clean$YRIMMIG) %>% 
  mutate(
    year_entry = ifelse(df_clean$ysm < df_clean$AGE - df_clean$EDUCD - 5, df_clean$YRIMMIG, df_clean$YRIMMIG + df_clean$EDUCD + 5))

The error I'm getting is Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "c('double', 'numeric')"

  • Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and https://stackoverflow.com/help/how-to-ask – GuedesBF Jul 05 '21 at 18:46
  • In your code, you are mutating a single-column data.frame/vector ( `as.numeric(df_clean$YRMMIG)`, whereas I think you probably want to mutate the whole dataframe, and convert YRMMIG to numeric after that. – GuedesBF Jul 05 '21 at 18:49

1 Answers1

0

The problem was 1) I was mutating a vector instead of a data frame 2) I should have tried the conversion to numeric separately.

library(dplyr)
df_clean$YRIMMIG <- as.numeric(df_clean$YRIMMIG) #integer to numeric
df_clean$AGE <- as.numeric(df_clean$AGE) # integer to numeric

df_clean %>% 
  mutate(
    year_entry = ifelse(df_clean$ysm < df_clean$AGE - df_clean$EDUCD - 5, df_clean$YRIMMIG, df_clean$YRIMMIG + df_clean$EDUCD + 5))