更广泛地旋转以解决重复项

发布于 2025-01-15 11:18:08 字数 1218 浏览 2 评论 0原文

我的数据集将单个申请号与一系列不同的申请人相关联。每个申请人居住的州都列在一栏中。

我想转换我的数据集,以便:

  • 申请编号列仅具有不同的值,
  • 每个州都有自己的列
  • 链接到申请的申请人数计入各自的州列,

例如

应用编号状态
600000AK
600000AK
600000AL
600000AL
600000AL
600001AK
600002MA
600002MA
600003AL
<块引用> <块引用> <块引用> <块引用>
申请号AKALMA
600000230
600001100
600002002
600003010

我认为它应该使用pivot_wider和计数函数,但我对R相对较新,仍然没有了解很多东西。

任何帮助或指示将不胜感激,谢谢:)

My dataset associates a single application number with a range of different applicants. The state in which each of these applicants lives is listed in a column.

I want to transform my dataset so that:

  • the application number column only features distinct values,
  • each state has its own column
  • the number of applicants linked to the application is counted in their respective state column

e.g.

appli.numberState
600000AK
600000AK
600000AL
600000AL
600000AL
600001AK
600002MA
600002MA
600003AL
application numberAKALMA
600000230
600001100
600002002
600003010

I thought it should use pivot_wider and count functions but I am relatively new to R and still haven't yet got my head around lots of stuff.

Any help or pointers would be greatly appreciated thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅倚清风 2025-01-22 11:18:08

怎么样:

  data <- tribble(
  ~appli.number,    ~State,
  600000,   "AK",
  600000,   "AK",
  600000,   "AL",
  600000,   "AL",
  600000,   "AL",
  600001,   "AK",
  600002,   "MA",
  600002,   "MA",
  600003,   "AL"
)


table(data)

或者如果你想使用pivot_wider()

library(tidyverse)    

data %>%
  group_by(appli.number, State)  %>%
  count()  %>%
  pivot_wider(names_from = "State", values_from = "n", values_fill = 0)

How about this:

  data <- tribble(
  ~appli.number,    ~State,
  600000,   "AK",
  600000,   "AK",
  600000,   "AL",
  600000,   "AL",
  600000,   "AL",
  600001,   "AK",
  600002,   "MA",
  600002,   "MA",
  600003,   "AL"
)


table(data)

or if you want to use pivot_wider()

library(tidyverse)    

data %>%
  group_by(appli.number, State)  %>%
  count()  %>%
  pivot_wider(names_from = "State", values_from = "n", values_fill = 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文