如何在不知道索引 R 的情况下在多列中应用 as.Date 函数
How to apply as.Date function in multi column without knowing the index R
想在不知道位置的情况下转换 class 个多列。
这是数据集
# Dataset name call : df . # It is a example , real data has many columns
# that you cannot have a clear index by one sight.
A.Date Price B.Date C.Date Size D.Date
2017-01-01 502 2017-01-03 2017-11-01 45.4 2016-10-01
2015-01-31 602 2017-02-03 2013-07-11 65.4 2016-03-24
我有如下代码:
df[,grepl("Date",colnames(df))] <-
lapply(df[,grepl("Date",colnames(df))],function(x) as.Date(x))
但是结果错误:
Error in strptime(x, f) : input string is too long
我什至试过这段代码:
DateCol <- grep("Date",names(df))
df[,c(DateCol)] <- as.Date(df[,c(DateCol)])
又出错了
as.Date.default(df[ c(DateCol)]) 错误:
'df[, c(DateCol)]' class 无法定义“日期”
代码有什么问题,解决方案是什么?
如果您的 xxx.Date 列是字符,则
library(dplyr)
txt <- "A.Date Price B.Date C.Date Size D.Date
2017-01-01 502 2017-01-03 2017-11-01 45.4 2016-10-01
2015-01-31 602 2017-02-03 2013-07-11 65.4 2016-03-24"
dat <- read.table(text = txt, header = TRUE)
res <- dat %>%
mutate_if(is.character, as.Date)
str(res)
> str(res)
'data.frame': 2 obs. of 6 variables:
$ A.Date: Date, format: "2017-01-01" ...
$ Price : int 502 602
$ B.Date: Date, format: "2017-01-03" ...
$ C.Date: Date, format: "2017-11-01" ...
$ Size : num 45.4 65.4
$ D.Date: Date, format: "2016-10-01" ...
由 reprex package (v0.2.0) 创建于 2018-03-27。
尽管@Tung 提供了一个很好的解决方案,但我仍然认为 dplyr::mutate_at
在这种情况下应该是更合适的选择,因为列预计将更改为 Date
包含 Date
作为其名称的一部分。因此,如果数据框包含其他 character
类型的列,那么 mutate_at
将提供选择列的灵活性。
grep("Date",names(.), value = TRUE)
提供列列表,其中包含 Date
作为其名称的一部分。
mutate_at
应用 as.Date
函数将这些列转换为 Date
类型。
library(dplyr)
df %>%
mutate_at(vars(grep("Date",names(.), value = TRUE)), funs(as.Date))
# A.Date Price B.Date C.Date Size D.Date
#1 2017-01-01 502 2017-01-03 2017-11-01 45.4 2016-10-01
#2 2015-01-31 602 2017-02-03 2013-07-11 65.4 2016-03-24
想在不知道位置的情况下转换 class 个多列。
这是数据集
# Dataset name call : df . # It is a example , real data has many columns
# that you cannot have a clear index by one sight.
A.Date Price B.Date C.Date Size D.Date
2017-01-01 502 2017-01-03 2017-11-01 45.4 2016-10-01
2015-01-31 602 2017-02-03 2013-07-11 65.4 2016-03-24
我有如下代码:
df[,grepl("Date",colnames(df))] <-
lapply(df[,grepl("Date",colnames(df))],function(x) as.Date(x))
但是结果错误:
Error in strptime(x, f) : input string is too long
我什至试过这段代码:
DateCol <- grep("Date",names(df))
df[,c(DateCol)] <- as.Date(df[,c(DateCol)])
又出错了 as.Date.default(df[ c(DateCol)]) 错误: 'df[, c(DateCol)]' class 无法定义“日期”
代码有什么问题,解决方案是什么?
如果您的 xxx.Date 列是字符,则
library(dplyr)
txt <- "A.Date Price B.Date C.Date Size D.Date
2017-01-01 502 2017-01-03 2017-11-01 45.4 2016-10-01
2015-01-31 602 2017-02-03 2013-07-11 65.4 2016-03-24"
dat <- read.table(text = txt, header = TRUE)
res <- dat %>%
mutate_if(is.character, as.Date)
str(res)
> str(res)
'data.frame': 2 obs. of 6 variables:
$ A.Date: Date, format: "2017-01-01" ...
$ Price : int 502 602
$ B.Date: Date, format: "2017-01-03" ...
$ C.Date: Date, format: "2017-11-01" ...
$ Size : num 45.4 65.4
$ D.Date: Date, format: "2016-10-01" ...
由 reprex package (v0.2.0) 创建于 2018-03-27。
尽管@Tung 提供了一个很好的解决方案,但我仍然认为 dplyr::mutate_at
在这种情况下应该是更合适的选择,因为列预计将更改为 Date
包含 Date
作为其名称的一部分。因此,如果数据框包含其他 character
类型的列,那么 mutate_at
将提供选择列的灵活性。
grep("Date",names(.), value = TRUE)
提供列列表,其中包含 Date
作为其名称的一部分。
mutate_at
应用 as.Date
函数将这些列转换为 Date
类型。
library(dplyr)
df %>%
mutate_at(vars(grep("Date",names(.), value = TRUE)), funs(as.Date))
# A.Date Price B.Date C.Date Size D.Date
#1 2017-01-01 502 2017-01-03 2017-11-01 45.4 2016-10-01
#2 2015-01-31 602 2017-02-03 2013-07-11 65.4 2016-03-24