通过添加带有动态名称和记录值的新列来展开数据框
我当前的数据框是格式:
name GRADUATION_RATE varname count
Example Name 1 13.0 Bus Stops 251
Example Name 1 13.0 Childcare Centers 87
Example Name 2 14.0 Bus Stops 300
Example Name 2 14.0 Childcare Centers 60
我想扩展数据框,以便基于变量名称的计数创建新列,因此将有与唯一的varnames数量一样多的新列。因此,上述预期输出将是:
name GRADUATION_RATE Bus Stops_count Childcare centers_count
Example Name 1 13.0 251 87
Example Name 2 14.0 300 60
我猜这就像Dplyr融化的相反,但我不确定该怎么做。
My current dataframe is of the format:
name GRADUATION_RATE varname count
Example Name 1 13.0 Bus Stops 251
Example Name 1 13.0 Childcare Centers 87
Example Name 2 14.0 Bus Stops 300
Example Name 2 14.0 Childcare Centers 60
I want to expand the dataframe such that it creates new columns based on the variable name's count, so there would be as many new columns as the number of unique varnames. So the expected output for the above would be:
name GRADUATION_RATE Bus Stops_count Childcare centers_count
Example Name 1 13.0 251 87
Example Name 2 14.0 300 60
I'm guessing this is like the opposite of a dplyr melt, but I'm not sure how to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
reshape2
(这也可以与
data.table :: dcast
一起使用,如果您有data.table
-Object。)tidyr
data
reshape2
(This also works with
data.table::dcast
if you have adata.table
-object.)tidyr
Data