简体   繁体   English

R - 如何根据包含日期序列的向量的值将列添加到数据框/data.table

[英]R - How can I add columns to a data frame / data.table based on values of a vector that contains a sequence of dates

I have a vector of dates我有一个日期向量

 start_date    <- as.Date("1990-01-01", "%Y-%m-%d")
 end_date      <- as.Date("2021-01-31", "%Y-%m-%d")

 new_cols      <- seq(from = start_date, 
                    to = end_date,
                    by = "month")
 new_cols      <- as.yearmon(new_col, frac = 1)
 new_col_names <- as.character(new_cols)

I have two other vectors one character vector that can be recycled when creating the data.frame and one integer vector.我还有另外两个向量,一个是在创建 data.frame 时可以回收的字符向量,另一个是 integer 向量。

ID.vec <- c(1:100)
type.vec <- c("A", "B", "C")

data.frame( rep(ID.vec, each = 3), type.vec, ?? )

Those date columns could be empty for all I care, as I am using a for loop to populate the table.这些日期列可能是空的,因为我正在使用 for 循环来填充表格。

How can I add all dates defined in the sequence as columns to the data.frame/data.table, so that...如何将序列中定义的所有日期作为列添加到 data.frame/data.table,以便...

ID     TYPE    2000-03-31  2000-04-30  2000-05-31  2000-06-30 etc
1      A       sth         sth         sth         sth
1      B       sth         sth         sth         sth
1      C       sth         sth         sth         sth
2      A       sth         sth         sth         sth
2      B       sth         sth         sth         sth
2      C       sth         sth         sth         sth
3      A       sth         sth         sth         sth
3      B       sth         sth         sth         sth
3      C       sth         sth         sth         sth

You can create the columns to add as folows您可以创建要添加为以下内容的列

df <- data.frame(rep(ID.vec, each = 3), type.vec)

someInitialValue <- 0
toAdd <- setNames(data.frame(matrix(rep(someInitialValue,NROW(df)*length(new_col_names)), 
                                    ncol = length(new_col_names), 
                                    nrow = NROW(df))), 
                  new_col_names)

and then bind them to your data.frame via然后通过将它们绑定到您的data.frame

newdf <- cbind(df,toAdd)

We can also do this with expand.grid我们也可以用expand.grid做到这一点

out <- expand.grid(ID.vec, type.vec)
out[new_cols] <- NA

暂无
暂无

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

相关问题 将日期序列添加到data.table(R) - Add sequence of dates to data.table (R) R - 在日期序列之间扩展一个值并作为列添加到 data.table - R - Expanding a value between a sequence of dates and add as columns to data.table r data.table序列日期 - r data.table sequence dates R,data.table:如何根据存储在字符向量中的其他列的名称来分配某些列的值? - R, data.table: How to assign values of some columns based on the names of other columns, which are stored in a character vector? R:如何根据数据框的值添加行? - R: How can I add rows based on values of a data frame? 如何在r data.table中找到包含任意值的向量的行的索引? - How can I find the index of rows that contain a vector of values in any order in an r data.table? R: Add boolean column to a data.table based on return values of a function which evaluates two columns from different data.table - R: Add boolean column to a data.table based on return values of a function which evaluates two columns from different data.table R data.table:如何计算组内向量列的值总和与表中列中其余值的比率? - R data.table: How to calculate ratio of sum of values, for columns from vector, within the group, vs. the rest of the values in the columns in table? 如何根据每周日期创建移动平均值,并按data.table中的多列分组? - How do I create a moving average based on weekly dates, grouped by multiple columns in data.table? 如何根据R中的向量从data.frame中提取值? - How can I extract values from a data.frame based on a vector in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM