简体   繁体   English

将多个字符列转换为R中的as.Date和time

[英]Convert multiple character columns to as.Date and time in R

We have an arbitrary dataset, called df: 我们有一个任意的数据集,称为df:

enter <- c("2017-01-01", "2018-02-02", "2018-03-03") 
guest <- c("Foxtrot","Uniform","Charlie","Kilo")
disposal <- c("2017-01-05", "2018-02-05", "2018-03-09") 
rating <- c("60","50","50")
date <- c("2017-04-10", "2018-04-15", "2018-04-20")
clock <- c("16:02:00", "17:02:00", "18:02:00")
rolex <- c("20:10:00", "20:49:00", "17:44:00") 
df <- data.frame(enter,guest,disposal,rating,date,clock,rolex, stringsAsFactors = F)

What I try to accomplish is to change the columns enter , disposal , and date from character to date, using dplyr package. 我试图完成的工作是使用dplyr包将输入处置日期列从字符更改为日期。 So, I came up with the following, simply by chaining it together: 因此,通过将它们链接在一起,我想到了以下内容:

library(dplyr)
library(chron)
df2 <- df %>% mutate(enter = as.Date(enter, format = "%Y-%m-%d")) 
%>% mutate(disposal = as.Date(disposal, format = "%Y-%m-%d")) 
%>% mutate(date = as.Date(date, format = "%Y-%m-%d"))

What I am after is this: which mutate function is needed from dplyr to get rid of the multiple chaining, ie when we have lots of columns with arbitrary namings that imply dates? 我所追求的是:dplyr需要哪个mutate函数才能摆脱多重链接,即当我们有很多带有任意名称的暗示日期的列时? I want to specify the columns by name, and then apply the as.Date function to change them from character to date. 我想通过名称指定列,然后应用as.Date函数将其从字符更改为日期。

Some solutions to different operations that are not applicable to this case: 不适用于这种情况的一些针对不同操作的解决方案:

1: convert column in data.frame to date 1: 将data.frame中的列转换为日期

2: convert multiple columns to dates with lubridate and dplyr 2: 使用lubridate和dplyr将多列转换为日期

3: change multiple character columns to date 3: 将多个字符列更改为日期

For example, I've tried, but with no luck: 例如,我尝试过,但是没有运气:

df2 <- df %>% mutate_at(data = df, each_of(c(enter, disposal, date)) = as.Date(format = "%Y-%m-%d"))

as given here: dplyr change many data types 如下所示: dplyr更改许多数据类型

As a bonus 作为奖励

Notice the clock and rolex columns. 注意clockRolex列。 Using the chron package simply converts them to the right format, ie time instead of character 使用chron包只需将它们转换为正确的格式,即时间而不是字符

df2 <- df %>% mutate(clock = chron(times = clock)) %>% mutate(rolex = chron(times = rolex))

As suggested here: convert character to time in r 如此处所建议: 将字符转换为r中的时间

Now, is the same solution available without all the chaining, especially when we have an arbitrary amount of columns with different namings etc.? 现在,没有所有链接就可以使用相同的解决方案,尤其是当我们有任意数量的具有不同命名等的列时吗?

You just need to tweak the arguments of mutate_at . 您只需要调整mutate_at的参数mutate_at Any additional arguments to as.Date are specified as arguments to mutate_at . as.Date所有其他参数都指定为mutate_at参数。

df2 <- df %>% mutate_at(vars(enter,disposal,date), as.Date, format="%Y-%m-%d")

The second part of your question has a similar solution. 问题的第二部分也有类似的解决方案。

df2 <- df2 %>% mutate_at(vars(clock, rolex), function(x) chron(times. = x))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM