[英]Apply date format in multiple dataframes
I have 3 data frames, as shown in the code below. 我有3个数据帧,如下面的代码所示。
code_1000 <-
as.data.frame(cbind(
c("3", "3", "7", "7", "7", "7", "2", "2", "4", "4"),
c("344", "344", "73", "73", "71", "72", "21", "27", "42", "43"),
c("9-02-2017", "10-01-2016","9-02-2014", "25-03-2015", "9-02-2017",
"10-06-2017", "8-04-2017", "25-08-2016", "07-08-2017", "15-11-2016"
)
))
code_2430 <-
as.data.frame(cbind(
c("3", "3", "7", "7", "7", "7", "2", "2", "4", "4"),
c("344", "344", "73", "73", "71", "72", "21", "27", "42", "43"),
c("9-02-2017", "10-01-2016","9-02-2014", "25-03-2015", "9-02-2017",
"10-06-2017", "8-04-2017", "25-08-2016", "07-08-2017", "23-09-2016"
)
))
code_3453 <-
as.data.frame(cbind(
c("3", "3", "7", "7", "7", "7", "2", "2", "4", "4"),
c("344", "344", "73", "73", "71", "72", "21", "27", "42", "43"),
c("9-02-2017", "10-01-2016","9-02-2014", "25-03-2015", "9-02-2017",
"10-06-2017", "8-04-2017", "25-08-2016", "07-08-2017", "13-06-2016"
)
))
names(code_1000) <- c("number", "code", "date")
names(code_2430) <- c("number", "code", "date")
names(code_3453) <- c("number", "code", "date")
I want to apply a date format on the column date
of each dataframe ( code_1000
, code_2430
and code_3453
). 我想在每个数据
code_1000
( code_1000
, code_2430
和code_3453
)的列date
上应用日期格式。 The desired date format is: 所需的日期格式为:
code_1000$date<-lubridate::dmy(as.character(code_1000$date)
Which gives a date format "yyyy-mm-dd" as output (see figure in the link below). 给出日期格式“ yyyy-mm-dd”作为输出(请参见下面链接中的图)。
The code above shows 3 samples to make it simpler. 上面的代码显示了3个示例,使其更简单。 Actually I have 50 dataframes, and I am using Shiny to plot some scatter graphs, which the x axis is the date column.
实际上我有50个数据帧,并且我正在使用Shiny绘制一些散点图,其中x轴是日期列。
USing for
, I tried the following code: for
,我尝试了以下代码:
list<- as.data.frame(c("1000","2430","3453"))
names(list) <- c("code.ID") # list of the codes dataframes ID
date.format<-function(df){
lubridate::dmy(as.character(df[,"date"]))
} # function to apply the desired date format
for (m in 1:nrow(list)){
loop.df<-eval(parse(text=paste0("code_",list$code.ID[m]))) # for each m, it returns a code_xxxx date frame
assign(loop.df[,3],date.format(loop.df)) # apply the date format on the dataframe, storing the results
}
I got the following error: 我收到以下错误:
Error in `[.default`(loop.df, , 3) : incorrect number of dimensions
When I apply the isolated date.format function on the dateframes, it works fine. 当我在日期框架上应用隔离的date.format函数时,它可以正常工作。
I would like to learn how to do this using for
and lapply()
function, as I have read that in R lapply()
is an easier approach most of times. 我想学习如何使用
for
和lapply()
函数来做到这一点,因为我已经读到在大多数情况下, lapply()
是一种更简单的方法。
Thank you in advance! 先感谢您!
<rant on>
I've been trying to get people to abandon the as.data.frame(cbind(...))
strategy for years. <rant on>
多年来,我一直在尝试使人们放弃as.data.frame(cbind(...))
策略。 It forces everything to be the same atomic type and then when that type happens to be character the results is all factors. 它迫使所有事物都具有相同的原子类型,然后当该类型恰好是字符时,结果就是所有因素。 A big mess in my opinion.
我认为这真是一团糟。 In this case there is a
dmy
method for factors, but some authors have not provided for that typical user expectation <rant off>
(Just use data.frame()
.) 在这种情况下,有一个
dmy
方法来处理因素,但是一些作者没有提供典型的用户期望<rant off>
(仅使用data.frame()
。)
Assemble the items with the first 5 characters "code_" in a character vector and then loop over them to build a list. 在字符向量中将前5个字符为“ code_”的项目组合在一起,然后循环遍历以构建列表。 Then loop (with
lapply
again) over that list of R objects to convert the 3rd column to date format: 然后在R对象列表上循环(再次
lapply
)将第3列转换为日期格式:
> objects(pattern="code_.+")
[1] "code_1000" "code_2430" "code_3453"
> obj_list <- lapply(objects(pattern="code_.+"), get)
> str(obj_list)
List of 3
$ :'data.frame': 10 obs. of 3 variables:
..$ number: Factor w/ 4 levels "2","3","4","7": 2 2 4 4 4 4 1 1 3 3
..$ code : Factor w/ 8 levels "21","27","344",..: 3 3 8 8 6 7 1 2 4 5
..$ date : Factor w/ 9 levels "07-08-2017","10-01-2016",..: 9 2 8 5 9 3 7 6 1 4
$ :'data.frame': 10 obs. of 3 variables:
..$ number: Factor w/ 4 levels "2","3","4","7": 2 2 4 4 4 4 1 1 3 3
..$ code : Factor w/ 8 levels "21","27","344",..: 3 3 8 8 6 7 1 2 4 5
..$ date : Factor w/ 9 levels "07-08-2017","10-01-2016",..: 9 2 8 5 9 3 7 6 1 4
$ :'data.frame': 10 obs. of 3 variables:
..$ number: Factor w/ 4 levels "2","3","4","7": 2 2 4 4 4 4 1 1 3 3
..$ code : Factor w/ 8 levels "21","27","344",..: 3 3 8 8 6 7 1 2 4 5
..$ date : Factor w/ 9 levels "07-08-2017","10-01-2016",..: 9 2 8 5 9 3 7 6 1 4
> obj_list <- lapply(obj_list , function(dfrm) {
dfrm[[3]] <- lubridate::dmy(as.character(dfrm[,"date"]))
dfrm} )
> str(obj_list)
List of 3
$ :'data.frame': 10 obs. of 3 variables:
..$ number: Factor w/ 4 levels "2","3","4","7": 2 2 4 4 4 4 1 1 3 3
..$ code : Factor w/ 8 levels "21","27","344",..: 3 3 8 8 6 7 1 2 4 5
..$ date : Date[1:10], format: "2017-02-09" "2016-01-10" ...
$ :'data.frame': 10 obs. of 3 variables:
..$ number: Factor w/ 4 levels "2","3","4","7": 2 2 4 4 4 4 1 1 3 3
..$ code : Factor w/ 8 levels "21","27","344",..: 3 3 8 8 6 7 1 2 4 5
..$ date : Date[1:10], format: "2017-02-09" "2016-01-10" ...
$ :'data.frame': 10 obs. of 3 variables:
..$ number: Factor w/ 4 levels "2","3","4","7": 2 2 4 4 4 4 1 1 3 3
..$ code : Factor w/ 8 levels "21","27","344",..: 3 3 8 8 6 7 1 2 4 5
..$ date : Date[1:10], format: "2017-02-09" "2016-01-10" ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.