[英]How do I add corresponding dates to a single column of values in r?
Let's say I have a single column vector dataframe like so:假设我有一个单列向量数据框,如下所示:
m <- matrix(sample(c(1:10), 400, replace = TRUE))
d <- as.data.frame(m)
I want to add another column to the dataframe on the left hand side with dates corresponding to each value.我想在左侧的数据框中添加另一列,日期与每个值相对应。 So for example, if I wanted to specify the date range be between 8th September 2020 and 13th September 2021. Then I want the resulting dataframe to look something like this:
例如,如果我想将日期范围指定为 2020 年 9 月 8 日和 2021 年 9 月 13 日之间。那么我希望生成的数据框如下所示:
date![]() |
value![]() |
---|---|
2020-09-09 ![]() |
x ![]() |
2020-09-10 ![]() |
y![]() |
... ![]() |
... ![]() |
2021-10-13 ![]() |
z ![]() |
How could I achieve this?我怎么能做到这一点?
Is the following what you are looking for?以下是您要查找的内容吗?
library(tidyverse)
library(lubridate)
m <- matrix(sample(c(1:10), 400, replace = TRUE))
d <- as.data.frame(m)
d %>%
mutate(Date=seq(ymd("2021-09-09"),by="days",length.out=400), .before=V1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.