I have a list data
.
and there are several data frames in each.
[[1]]
ID: int [1:100] ...
Date: Factor w/ ...
days: num [1:100] ...
[[2]]
ID: int [1:100] ...
Date: Factor w/ ...
like this.
And I want to convert that factor to Date format.
I thought about
unlist the list - changing format - making it to list again.
But I have no idea how to do that..
sapply(data, function(x) x$Date <- as.Date(x$Date))
This doesn't work. It only returns Date and doesn't change the data type. Is there any fast way to convert that format?
I can solve this by using for loop.
for(i in 1:2){
data[[i]]$Date <- as.Date(data[[i]]$Date)}
But I would like to use sapply or lappy.
It is better to transform factor
into character
at first and then to Date
format. The most easiest way is to use lubridate
package. ymd
transform character
vectors of format e.g. 2018-11-22
into Year-Month-Date datetime object. Please pay attention to lambda-function body, after the change of the data frame it is typed x
, which is a shortcut of return(x)
. See the code below:
library(lubridate)
# simulation of data
df1 <- data.frame(
ID = 1:100,
Date = as.factor(sample(seq(ymd("2018-01-01"), ymd("2018-12-01"), 1), 100)),
days = sample(100))
df2 <- data.frame(
ID = 1:100,
Date = as.factor(sample(seq(ymd("2018-01-01"), ymd("2018-12-01"), 1), 100, replace =TRUE)))
dfs <- list(df1, df2)
str(dfs)
# List of 2
# $ :'data.frame': 100 obs. of 3 variables:
# ..$ ID : int [1:100] 1 2 3 4 5 6 7 8 9 10 ...
# ..$ Date: Factor w/ 100 levels "2018-01-06","2018-01-10",..: 17 89 40 2 84 46 58 62 66 43 ...
# ..$ days: int [1:100] 50 4 19 6 33 47 95 25 13 5 ...
# $ :'data.frame': 100 obs. of 2 variables:
# ..$ ID : int [1:100] 1 2 3 4 5 6 7 8 9 10 ...
# ..$ Date: Factor w/ 87 levels "2018-01-03","2018-01-04",..: 3 30 61 6 78 34 5 71 49 55 ...
# handling the data
dfs_2 <- lapply(dfs, function(x) {
x$Date <- ymd(as.character(x$Date))
x
})
str(dfs_2)
# List of 2
# $ :'data.frame': 100 obs. of 3 variables:
# ..$ ID : int [1:100] 1 2 3 4 5 6 7 8 9 10 ...
# ..$ Date: Date[1:100], format: "2018-03-10" "2018-10-25" "2018-11-25" ...
# ..$ days: int [1:100] 7 99 75 91 30 78 9 82 15 37 ...
# $ :'data.frame': 100 obs. of 2 variables:
# ..$ ID : int [1:100] 1 2 3 4 5 6 7 8 9 10 ...
# ..$ Date: Date[1:100], format: "2018-05-30" "2018-05-20" "2018-05-13" ...