将R中的每个条目的列转换为多行 [英] Convert columns into multiple rows per entry in R

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

问题描述

我有以下数据:


word   Jan-2013  Feb-2013  Mar-2013
A      1         2         3 
B      5         2         4

我想将多个日期列转换为一个名为date的日期,并为该值添加另一列.

I want to convert the multiple date columns into one, named date and add an additional column for the value.


word date       value
A    Jan-2013   1
A    Feb-2013   2
A    Mar-2013   3
B    Jan-2013   5
B    Feb-2013   2
B    Mar-2013   4

有人可以协助吗?

谢谢

推荐答案

其他R选项

除了Metrics的答案外,这还有R的两个附加选项(假设您的data.frame称为"mydf"):

Additional R options

In addition to Metrics's answer, here are two additional options for R (assuming your data.frame is called "mydf"):

cbind(mydf[1], stack(mydf[-1]))

library(reshape)
melt(mydf, id.vars="word")

Excel选项

我不是Excel用户,但是由于此问题也被标记为"Excel",因此我建议对于您的示例,这非常简单:

For your example, it's pretty straightforward:

  1. 在安装并激活附件后,转到"Tableau"菜单.

  1. Go to the "Tableau" menu after installing the add-on and activating it.

选择包含要取消堆叠的值的单元格.单击确定.

Select the cells which contain the values you want to unstack. Click on OK.

查看结果.

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

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