将县数据汇总到州数据

发布于 2025-02-13 20:42:42 字数 677 浏览 0 评论 0原文

我有一个县数据集,我想将其转换为整个州的汇总数据。它描述了每个县的案件,该案件是我想在日期之前出现州案件的日期。

例如,对于日期和案例,第一个县数据框架可能看起来像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文