将县数据汇总到州数据
我有一个县数据集,我想将其转换为整个州的汇总数据。它描述了每个县的案件,该案件是我想在日期之前出现州案件的日期。
例如,对于日期和案例,第一个县数据框架可能看起来像这样:
data_sample <- data.frame(date = c("2020-01-01", "2020-01-02", "2020-01-03", "2020-01-01", "2020-01-02", "2020-01-03"), cumulative_cases = c(4,6,10,3,5,11))
我尝试编写自己的功能来解决问题来解决每个日期和累积案例的新数据框架,然后添加所有匹配的情况该日期是第一个数据帧。不幸的是,匹配不起作用。
case_date_aggregator <- function(common_date){
for (x in common_date){
if (x == cumulative_cases_df$Dates[x]){
cumulative_cases_df$Cases[x] <- cumulative_cases_df$Cases[x] + county_covid_data$cumulative_cases[x]
}}
}
case_date_aggregator(county_covid_data)
有人可以解释为什么吗?
I have a county dataset that I want to convert into aggregate data for the whole state. It describes COVID cases for each county by date that I want to have total state cases by date.
For example, the first county dataframe might look like this for dates and cases:
data_sample <- data.frame(date = c("2020-01-01", "2020-01-02", "2020-01-03", "2020-01-01", "2020-01-02", "2020-01-03"), cumulative_cases = c(4,6,10,3,5,11))
I tried writing my own function to solve the issue by making a new dataframe with only one row of each date and cumulative cases, then adding up all the cases that match that date from the first dataframe. Unfortunately the matching is not working.
case_date_aggregator <- function(common_date){
for (x in common_date){
if (x == cumulative_cases_df$Dates[x]){
cumulative_cases_df$Cases[x] <- cumulative_cases_df$Cases[x] + county_covid_data$cumulative_cases[x]
}}
}
case_date_aggregator(county_covid_data)
Can someone explain why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论