简体   繁体   English

如何将相应的日期添加到 r 中的单列值?

[英]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 2020-09-09 x X
2020-09-10 2020-09-10 y
... ... ... ...
2021-10-13 2021-10-13 z 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.

相关问题 R:如何根据带有日期的列添加带有天数的列 - R: How do I add a column with day number based on a column with dates 如何根据R中的匹配ID添加列值? - How do I add column values based on matching IDs in R? 对应于 R 中现有单列的数值的新列 - New column of number values corresponding to an existing single column in R 如何匹配两个不同表的列中的值以提取 R 中第二个表的不同列中的对应值? - How do I match values in columns of two different tables to extract corresponding values in a different column of the second table in R? 如何添加具有与另一列对应的值的列? - How to add a column with values corresponding to another column? R-如果值在A列中匹配,那么它们在B列中对应的值匹配的频率是多少? - R - if values match in column A, how often do their corresponding values in column B match? R:如何获取对应列的值? - R: How to obtain corresponding column of values? 使用R中的data.table,如何有效替换单个列中命名的多个列值? - With data.table in R, how do I efficiently replace multiple column values named within a single column? 如何在 R 的列中为日期添加午夜时间戳? - How can I add a midnight time stamp to dates in a column in R? dplyr 过滤器 - R 的全新过滤器,如何从单个列中过滤掉多个值 - dplyr filter-brand new to R, how do I filter out multiple values from a single column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM