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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…