在不知道变量名的情况下将数据框重塑为三列宽

时间:2019-03-31 22:19:18

标签: r reshape

我的数据框的列名未知,但格式一致。如何在不使用列名的情况下将其重塑为三列宽?

COLUMN "Highest Rate" FORMAT $99.99
COLUMN "Lowest Rate" FORMAT $99.99
COLUMN "Average Rate respectively" FORMAT $99.99

SELECT MAX(hourlyrate) "Highest Rate",
       MIN(hourlyrate) "Lowest Rate",
       AVG(hourlyrate) "Average Rate respectively"
FROM charges;

目标

cpu[,1:6]
  Datapoints.Timestamp Datapoints.Maximum Datapoints.Unit Datapoints.Timestamp.1 Datapoints.Maximum.1 Datapoints.Unit.1
1 2019-03-05T08:00:00Z           7.833333         Percent   2019-03-11T22:00:00Z                24.25           Percent

数据集:

Timestamp             Maximum   Unit
2019-03-05T08:00:00Z  7.833333  Percent
.....

1 个答案:

答案 0 :(得分:0)

do.call(rbind, lapply(seq(1, NCOL(df1), 3), function(i)
    setNames(df1[,i+(0:2)], colnames(df1)[1:3])))
#  Datapoints.Timestamp Datapoints.Maximum Datapoints.Unit
#1 2019-03-05T08:00:00Z           7.833333         Percent
#2 2019-03-11T22:00:00Z          24.250000         Percent