Ben
Ben

Reputation: 69

R - Apply custom function to single column row by row

I have created a custom function and wish to apply it to a single column of a dataframe row by row, then assign it back to the original column

The custom function is below, and aims to fix the dates in an excel file.

format_dates = function(x) {
  x = trimws(x)
  if ( grepl('/', x, fixed=TRUE) ) {
    as.Date(x, tryFormats = c("%d/%m/%Y", "%m/%d/%Y"))
  } else {
    tryCatch(
      { as.Date(as.integer(x), origin='1899-12-30') },
             warning=function(x) { return( NA ) 
             } )
  }
}

It is mandatory to do this row by row. I have searched high and low and I have seen many replies using lapply, apply, and sapply but they do not work. As an example, I tried:

df$Child_Date_of_Birth = apply(df$Child_Date_of_Birth, 2, format_dates)

With the result of

Error in apply(df$Child_Date_of_Birth, 2, format_dates) : 
  dim(X) must have a positive length

This is frustrating, as in Pandas you can simply run

df['Child_Date_of_Birth'] = df['Child_Date_of_Birth'].apply(format_dates)

but in R this becomes the most obscure thing ever??

Anyone able to enlighten me... will appreciate it

Upvotes: 0

Views: 300

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389275

An example data would be helpful but I think you can try sapply :

df$Child_Date_of_Birth <- sapply(df$Child_Date_of_Birth, format_dates)

Upvotes: 2

Related Questions