Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

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

question from:https://stackoverflow.com/questions/65934730/r-apply-custom-function-to-single-column-row-by-row

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...