R中数据帧的转换结构 [英] Transform structure of data frame in R

查看:47
本文介绍了R中数据帧的转换结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下列的数据框:

I have a data frame with the following columns:

**Columns   Country   Year   Number_of_deaths**
**Data**      US      2000    25
              US      2001    30
              UK      2000    30
              UK      2001    21

我要将其转换为以下格式:

I want to convert this to the following format:

**Columns: Country   2000   2001   2002   2003   2004**
**Data**     US        25     30     35     40     25
             UK        30     21     21     23     45

有人可以给我R中的示例代码吗?任何包装都可以。会感谢您的帮助。

Can somebody give me sample code in R to do this? Any package is fine. Your help will be appreciated.

推荐答案

使用此方法:

希望它有效

library(reshape2)
dcast(data,country~year,value.var="No_of_deaths")

输出:

  country 2000 2001
1      UK   30   21
2      US   25   30

谢谢

这篇关于R中数据帧的转换结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆