0

When attempting to create a new column using a user created function, I receive an 'unsupported class error'. Goal is to create a new column using mutate, stored in a data.frame by using the created function applying to all rows (3 rows in this example...).

Testing the function itself, I can pass the 2 inputs with the correct output retrieved (example below. Note the function consumes 2 parameters and 'lookups' the value in a 3 column table 'LFPR', which is a 3 column table containing Sex, Age, and Labor Force participation rate. Interestingly, the output from the function example is a tibble - is mutate having issues with the output being a tibble?

M5_DCF_Setup_Test <- M5_DCF_Setup %>%
  filter(M5_DCF_Setup$Person_Id_M5<=3) %>%
  rowwise() %>%
  mutate(lfpr = Lfpr_Lu(Person_Sex_M5, Person_Age_M5))

Error: Column `lfpr` is of unsupported class data.frame

#Function
Lfpr_Lu <- function (S,A){
  LFPR_Percent <- LFPR %>%
    filter(Sex==S & Age==A) %>%
    select(LFPR_Percent)
  return(LFPR_Percent)
}
#Test
Lfpr_Lu("M",24)

# A tibble: 1 x 1
  LFPR_Percent
         <dbl>
1        0.689
  • You'll need to put it into a list `mutate(lfpr = list(Lfpr_Lu(Person_Sex_M5, Person_Age_M5)))` In the future it's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. If we can't copy/paste the code to run and test it, it's much harder to see what's going on. – MrFlick Feb 07 '22 at 20:05

0 Answers0