Reputation: 1252
Let's say you have several CSV tables like
Foo Date1 Date2
bar 2010-03-05 2023-09-09
...
[table1]
or
Foobar Date
baz 2042-01-01
[table2]
After parsing those tables using read.csv, I'd like to convert the colulumns containing Dates to the internal date type.
One could do this with
table1$Date1 <- as.Date(table1$Date1, "%Y-%m-%d")
table1$Date2 <- as.Date(table1$Date2, "%Y-%m-%d")
table2$Date <- as.Date(table2$Date, "%Y-%m-%d")
But that produces a lot of redundant code, so I'd like to write a function that takes a table and a variadic number of columns and rearranges the table. Just something convenient like:
convertDate(table1, Date1, Date2)
convertDate(table2, Date)
But how to define convertDate?
I already tried (in a simple one-argument case) sth. like:
convertDate <- function(table, column) {
table[[column]] <- as.Date(table[[column]], "%Y-%m-%d")
}
But this doesn't work, as R apparently passes the table argument by value and not by reference...
What's the most R-like way to solve this issue?
Thanks
Upvotes: 0
Views: 82
Reputation: 2715
lapply would work here
tbl <- data.frame(bar = 1, Date1 = "2010-03-05", Date2 = "2023-09-09", stringsAsFactors = FALSE)
str(tbl)
# 'data.frame': 1 obs. of 3 variables:
# $ bar : num 1
# $ Date1: chr "2010-03-05"
# $ Date2: chr "2023-09-09"
tbl[, -1] <- as.data.frame(lapply(tbl[, -1], as.Date))
str(tbl)
# 'data.frame': 1 obs. of 3 variables:
# $ bar : num 1
# $ Date1: Date, format: "2010-03-05"
# $ Date2: Date, format: "2023-09-09"
As a function:
convertDate <- function(table, columns) {
table[, columns] <- as.data.frame(lapply(table[, columns, drop = FALSE], as.Date))
return(table)
}
Upvotes: 1
Reputation: 157
You have to pass a list of column names and iterate over it. I am attaching sample code below.
convertDate <- function(table, columns) {
for (column in columns){
table[[column]] <- as.Date(table[[column]], "%Y-%m-%d")
}
return(table)
}
table2 = data.frame(Date='2042-01-01')
class(table2$Date)
table2 = convertDate(table2, 'Date')
class(table2$Date)
table1 = data.frame(Date1='2010-03-05', Date2='2023-09-09')
class(table1$Date1)
table1 = convertDate(table1, list('Date1','Date2'))
class(table1$Date1)
class(table1$Date2)
Upvotes: 1