gpt4 book ai didi

Date data- type in R programing(R编程中的日期数据类型)

转载 作者:bug小助手 更新时间:2023-10-24 21:08:40 24 4
gpt4 key购买 nike



I am currently working on Google data analytic capstone cyclistic.
I am to add a column called date, day, month and year.
I have install necessary packages.
I have a column called $started_at which contain date and time in this format "4/26/2020 17:45" and it is in chr (character)
And I type the code like this

我目前正在做谷歌数据分析的Capstone Cycle。我要添加一个名为Date、Day、Month和Year的列。我已经安装了必要的程序包。我有一个名为$started_at的列,其中包含日期和时间,格式为“4/26/2020 17:45”,格式为chr(字符),我键入的代码如下所示


df$date<-as.Date(df$started_at)

The output said
Error in chartodate(x)
Character string is not in a standard unambiguous format

在ChartoDate(X)字符串中输出所述错误不是标准明确格式


I also tried

我也试过了


df$date<-as.Date(df$started_at,format = "%y%m%d")

The date column brings NA

Date(日期)栏显示NA


I also tried

我也试过了


df$started_at = strptime(df$started_at,"%y-%m-
%d %H: %M: %S")

The output of this is also NA

该命令的输出也是NA


What Is the problem

有什么问题吗?


I am currently working on Google data analytic capstone cyclistic.

我目前正在做谷歌数据分析的Capstone Cycle。


I am to add a column called date, day, month and year.

我要添加一个名为Date、Day、Month和Year的列。


I have install necessary packages.

我已经安装了必要的程序包。


I have a column called $started_at which contain date and time in this format "4/26/2020 17:45" and it is in chr (character)

我有一个名为$STARTED_AT的列,其中包含日期和时间,格式为“4/26/2020 17:45”,格式为chr(字符)


And I type the code like this

然后我像这样输入代码



df$date<-as.Date(df$started_at)


The out put said

该组织说,


Error in chartodate(x)

图表日期错误(X)


Character string is not in a standard unambiguous format

字符串不是标准的明确格式


I also tried

我也试过了



df$date<-as.Date(df$started_at,format = "%y%m%d")


The date column brings NA

Date(日期)栏显示NA


I also tried

我也试过了



df$started_at = strptime(df$started_at,"%y-%m-

%d %H: %M: %S")


The out put of this is also NA

这方面的产出也是天方夜谭


What Is the problem

有什么问题吗?


更多回答
优秀答案推荐

The issue is that your started_at column is a datetime, not a date. To convert it to a date, you have to pass in the format, using the strptime formatting syntax:

问题是Started_at列是一个日期时间,而不是一个日期。要将其转换为日期,必须使用strptime格式语法传入格式:


df$started_at <- as.POSIXct(df$started_at, format = "%m/%d/%Y %H:%M") |> as.Date()
# or
df$started_at <- as.Date(df$started_at, format = "%m/%d/%Y %H:%M")
#or
library(lubridate)
mdy_hm(df$started_at) |> as_date()

Data:

数据:


df <- data.frame(
started_at = "4/26/2020 17:45")

更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com