[英]Convert R data frame to time series
Hello I have been trying to use xts to convert a dataframe to time series my understanding is I need to first convert the Date column that is currently set character type to a date type my data set has about 250 rows I was hoping to access the column that contains the date data then run a function to convert char to date. 您好我一直在尝试使用xts将数据帧转换为时间序列我的理解是我需要首先将当前设置字符类型的Date列转换为日期类型我的数据集有大约250行我希望访问该列包含日期数据,然后运行将char转换为日期的函数。 Any advice would be helpful thank you.
任何建议都会有所帮助,谢谢。
str(gas)
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 253 obs. of
5 variables:
$ Date : chr "8/22/2017" "8/23/2017" "8/24/2017" "8/25/2017" ...
$ Google.Close : num 925 927 921 916 914 ...
$ Amazon.Close : num 967 958 952 945 946 ...
$ Google.Return: num NA 0.0025 -0.00617 -0.00585 -0.00227 ...
$ Amazon.Return: num NA -0.009205 -0.005793 -0.007549 0.000804 ...
- attr(*, "spec")=
.. cols(
.. Date = col_character(),
.. Google.Close = col_double(),
.. Amazon.Close = col_double(),
.. Google.Return = col_double(),
.. Amazon.Return = col_double()
.. )
You can do that with the as.Date
function. 您可以使用
as.Date
函数来实现。 You will need to specify the format of the dates. 您将需要指定日期的格式。 In your case, that seems to be
%m/%d/%Y
. 在你的情况下,这似乎是
%m/%d/%Y
。 You can read about these formats on the help page ?strptime
. 您可以在帮助页面
?strptime
上阅读有关这些格式的?strptime
。
gas$Date = as.Date(gas$Date, format="%m/%d/%Y")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.