将时间信息汇总到动态矩阵中
有一个数据框
id <- c("A", "A", "A", "A", "B", "B", "B", "C", "C", "D", "D", "E")
year <- c("2005", "2006", "2007", "2008", "2005", "2006", "2007", "2005", "2007", "2006", "2007", "2008")
value <- 1:12
df <- data.frame(id, year, value)
我 ,按年。行计算到连续一年的“生存”多少个ID:
id_observed <- matrix(c(3,2,3,1,0,1,1,0,0,0,0,0,0,0,0,1), nrow = 4, ncol = 4)
#First observed id's (by columns), consecutive id's observations (by rows)
colnames(id_observed) <- c("2005", "2006", "2007", "2008")
rownames(id_observed) <- c("2005", "2006", "2007", "2008")
id_observed
适用于生成矩阵value_observed
从value
中获取信息。列计算第一次观察到的ID的汇总值。行计算“生存”到连续一年的ID的汇总值:
value_observed <- matrix(c(14,8,19,4,0,10,11,0,0,0,0,0,0,0,0,12), nrow = 4, ncol = 4)
#First observed value (by columns), consecutive value's observations (by rows)
colnames(value_observed) <- c("2005", "2006", "2007", "2008")
rownames(value_observed) <- c("2005", "2006", "2007", "2008")
value_observed
关于如何构建矩阵id_observed
和value_observed
的任何线索?
I have a data frame like df
:
id <- c("A", "A", "A", "A", "B", "B", "B", "C", "C", "D", "D", "E")
year <- c("2005", "2006", "2007", "2008", "2005", "2006", "2007", "2005", "2007", "2006", "2007", "2008")
value <- 1:12
df <- data.frame(id, year, value)
I want to convert df
into a matrix id_observed
where columns count how many id's are observed for the first time, by year. Rows count how many ids "survive" to the consecutive year:
id_observed <- matrix(c(3,2,3,1,0,1,1,0,0,0,0,0,0,0,0,1), nrow = 4, ncol = 4)
#First observed id's (by columns), consecutive id's observations (by rows)
colnames(id_observed) <- c("2005", "2006", "2007", "2008")
rownames(id_observed) <- c("2005", "2006", "2007", "2008")
id_observed
The same idea applies to generate matrix value_observed
taking the information from value
. Where columns count the aggregated value of id's that are observed for the first time, by year. Rows count the aggregated value of the ids that "survived" to the consecutive year:
value_observed <- matrix(c(14,8,19,4,0,10,11,0,0,0,0,0,0,0,0,12), nrow = 4, ncol = 4)
#First observed value (by columns), consecutive value's observations (by rows)
colnames(value_observed) <- c("2005", "2006", "2007", "2008")
rownames(value_observed) <- c("2005", "2006", "2007", "2008")
value_observed
Any clue on how to build matrices id_observed
, and value_observed
in an automatic way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建此功能,
get_matrix()
,该功能利用整理的方法在唯一的年份中循环,每年创建数据,绑定行,然后转移更广泛的用法
输出
输出
输出输出
更新:
数据。表选项
输出:
You can create this function,
get_matrix()
, which leverages tidyverse approach to loop over unique years, creating the data for each year, binding the rows, and then pivoting widerUsage
Output
Usage
Output
Update:
data.table option
Output: