R编程将数据列转换为行 [英] R Programming Converting data columns to Rows

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

问题描述

我有一个具有以下结构的数据框:

I have a dataframe which had the following structure:

Date            Year          quantity
19-JUN-15   2022            958
19-JUN-15   2021            894
18-JUN-15   2020            80
18-JUN-15   2019            96

等。

我想将其转换为以下形式:

I want to transform this to look like this :

    Date           2022       2021     2020     2019                     
19-JUN-15          958        894
18-JUN-15                               80       96

,依此类推。基本上,我想使第1列给出日期,其余各列分别是年份和根据坐标匹配的数量。我该如何实现?

and so on. Basically I want to make column 1 giving the dates and the rest of the columns are the individual years and the quantity matched according to the coordinates. How do I achieve this?

推荐答案

您可以尝试

library(reshape2)
dcast(df1, Date~Year, value.var='quantity')

library(tidyr)
spread(df1, Year, quantity)

 xtabs(quantity~Date+Year, df1)

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

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