每N列分割数据并使用R rbind [英] Split Data Every N Columns and rbind Using R

查看:43
本文介绍了每N列分割数据并使用R rbind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框。在我的数据集中。 a,a1和a2是完全相同的变量。但是,当您在r中具有相同的名称时,它将在名称末尾自动添加1。

I have a data frame. In my data set. a, a1 and a2 are the exact same variable. However when you have the same names in r it automatically adds a 1 at the end of the name.

  df = data.frame(a = rnorm(4), b = rnorm(4), c = rnorm(4), a1 = rnorm(4), b1 = rnorm(4), c1 = rnorm(4), a2 = rnorm(4),
                b2 = rnorm(4), c2 = rnorm(4), date = seq(as.Date("2019-05-05"),as.Date("2019-05-08"), 1))
  print(df)



             a          b          c         a1         b1         a2         b2         c2       date
1 -1.0938097  1.3948486  1.2805904  1.6187439  1.0200681 -1.4335761 -0.4583526  0.3825598 2019-05-05
2 -0.3195004 -1.1281779 -2.1905902 -1.1693616 -0.9612850 -0.7502631 -0.5637997  0.3072459 2019-05-06
3 -0.2135026  0.7015042 -0.8271073 -0.1115213 -1.0378507  0.5620332 -2.0615450  1.7363142 2019-05-07
4  1.0413566 -1.1983207  0.9262545  0.6454741 -0.7874252  0.1904461  0.8970132 -1.4173619 2019-05-08

我想将此数据转换为长格式,然后将数据帧拆分为多个块( a1-c1,a2-c2),然后重新绑定每个子集。在data.frame的末尾有一个称为date的关键列。

I would like to turn this data into long format where I split the data frame into chunks (a-c, a1-c1, a2-c2) and then rbind each subset. There is a key column called date at the end of the data.frame.

我希望表格成为以下内容。

I would like the table to become the following.

       a          b          c       date
1   1.70236896  0.1847794  1.0642016 2019-05-05
2  -1.84604746  1.1229081  1.0550992 2019-05-06
3  -0.70185143 -0.8527223  1.3261573 2019-05-07
4  -0.47930296  0.2822001 -0.3271825 2019-05-08
5  -0.09950265 -0.1881748 -0.7482557 2019-05-05
6   0.72087483  2.0053211  1.1154889 2019-05-06
7  -1.83254875 -0.4060090 -0.2664467 2019-05-07
8  -0.17379130  0.6302901  1.5287194 2019-05-08
9   1.72706128 -1.4701842  1.1615761 2019-05-05
10  2.00246599  0.1306764 -1.8767190 2019-05-06
11  0.05263048  0.1173080  0.4293342 2019-05-07
12 -0.70024619  1.0677009 -0.2974141 2019-05-08

谢谢。

推荐答案

data.table 中的融化采取多个措施 模式

library(data.table)
melt(setDT(df), measure = patterns("^a", "^b", "^c"),
         value.name = c("a", "b", "c"))[, variable := NULL][]

这篇关于每N列分割数据并使用R rbind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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