Home > Net >  Using grepl beyond a 'this or that' condition
Using grepl beyond a 'this or that' condition

Time:10-17

I have a dataframe of the following pattern: tibble [9 x 2] (S3: tbl_df/tbl/data.frame)

 $ Date: chr [1:9] "Tuesday 4 October 2022" "Wednesday 5 October 2022" "Thursday 6 October 2022" "Friday 7 October 2022:"
 $ SIC       : chr [1:9] "01500" "01610" "01629" "01630"

I'd like to use grepl to convert the 'SIC' to its appropriate classification in words as a new column named "Type". 01500 = "Mixed farming", 01610 = "Support activities for crop production", 01629 = " Support activities for animal production (other than farm animal boarding and care) n.e.c.", 01630 = Post-harvest crop activities"

Using grepl, I've got as far as doing:

df$Type <- ifelse(grepl("01500", df$SIC), "Mixed farming", "Other")

And that ofc only works for one specific case. Does anyone know how to extend it out to far more conditions?

CodePudding user response:

This is a case where a named array (the R equivalent of a dictionary) works really well. Note the use of quotes on the names (this is necessary because they are numeric)

dict <- c(
  "01500" = "Mixed farming", 
  "01610" = "Support activities for crop production", 
  "01629" = "Support activities for animal production (other than farm animal boarding and care) n.e.c.", 
  "01630" = "Post-harvest crop activities"
)

df$Type <- dict[df$SIC]
  • Related