How do I write a function that converts character vector to date vector in R? -
this seems simple enough function write, think i'm misunderstanding requirements formal arguments / how r parses , evaluates function.
i'm trying write function converts character vector of form "%m/%d/%y" (and belonging data.frame df) date vector, , formats "%m/%d/%y", follows:
dateformat <- function(x) { df$x <- (format(as.date(df$x, format = "%m/%d/%y"), "%m/%d/%y")) } i thinking that...
dateformat(a) ... take "a" actual argument x , plug function, resolving as:
df$a <- (format(as.date(df$a, format = "%m/%d/%y"), "%m/%d/%y")) however, following error when running dateformat(a):
error in as.date.default(df$x, format = "%m/%d/%y") : not know how convert 'df$x' class “date” can please explain why understanding of formal/actual arguments and/or r function parsing/evaluation incorrect? thank you.
update
of course, variables want convert dates (e.g., df$a, df$b, df$c), write
df$a <- (format(as.date(df$a, format = "%m/%d/%y"), "%m/%d/%y"))
df$b <- (format(as.date(df$b, format = "%m/%d/%y"), "%m/%d/%y"))
df$c <- (format(as.date(df$c, format = "%m/%d/%y"), "%m/%d/%y"))
but i'm looking improve coding skills making more general function feed vector of variables. instance, if had df$a df$z, character variables wanted convert date variables? after write proper function, i'd perhaps run so:
for (n in letters) { dateformat(n) }
first, format(...) function returns character vector, not date, if x string,
format(as.date(x, format = "%m/%d/%y"), "%m/%d/%y") converts x date , character, in:
result <- format(as.date("01/03/2014", format = "%m/%d/%y"), "%m/%d/%y") result # [1] "01/03/2014" class(result) # [1] "character" second, referencing object, such df, in function, on lhs of expression, causes r create object in scope of function.
a <- 2 f <- function(x) <- x f(3) # [1] 2 here, set variable, a, 2. in function create new variable, a in scope of function, set x (3), , destroy when function returns. in global environment a still 2.
if insist on using dateformat(...) function, should work work:
df <- data.frame(a=paste("01",1:10,"2014",sep="/"), b=paste("02",11:20,"2014",sep="/"), c=paste("03",21:30,"2014",sep="/")) dateformat <- function(x) as.date(df[[x]], format = "%m/%d/%y") (n in letters[1:3]) df[[n]] <- dateformat(n) sapply(df,class) # b c # "date" "date" "date" this more efficient though:
df <- as.data.frame(lapply(df,as.date,format="%m/%d/%y"))
Comments
Post a Comment