r - 在 Azure ML Studio 中转置和添加 R 中的列

标签 r azure transpose

我在 Azure 中获取以下数据集。每行都是与预测模型相关的参数。

我对 R 比较陌生。我尝试了以下代码,但它没有给我预期的输出。转置数据集后,我想添加一个附加列“月-年”。

有人可以帮助我吗?谢谢。

数据集

features    V1      V2      V3      V4      V5      V6      V7      V8      V9      V10     V11     V12
A           28.21   42.03   48.56   46.85   46.03   54.6    63.87   50      53.34   43.47   34.66   27.48
B           1333    1348.64 1364.28 1379.92 1395.56 1411.2  1426.84 1442.48 1458.11 1473.75 1489.39 1505.03
C           10.05   5.46    4.82    5.27    5.07    4.07    9.53    1.95    6.95    6.54    5.91    0.56
D           18.22   18.41   14.31   30.28   18.16   15.52   12.52   13.14   15.05   8.89    12.51   25.25

R代码

# Map 1-based optional input ports to variables
dataset <- maml.mapInputPort(1) 

a <- c("A", "B", "C", "D")

data.set <- cbind(a, dataset)
names(data.set)[1] <- c("features")

# first remember the names
n <- dataset$features

# transpose all but the first column (name)
df.aree <- as.data.frame(t(data.set[,-1]))
names(data.set)[1] <- n

df.aree$myfactor <- factor(row.names(df.aree))

maml.mapOutputPort("df.aree")

预期结果

Month-Year  A       B           C       D
01-01-15    28.21   1333        10.05   18.22
01-02-15    42.03   1348.64     5.46    18.41
01-03-15    48.56   1364.28     4.82    14.31
01-04-15    46.85   1379.92     5.27    30.28
01-05-15    46.03   1395.56     5.07    18.16
01-06-15    54.6    1411.2      4.07    15.52
01-07-15    63.87   1426.84     9.53    12.52
01-08-15    50      1442.48     1.95    13.14
01-09-15    53.34   1458.11     6.95    15.05
01-10-15    43.47   1473.75     6.54    8.89
01-11-15    34.66   1489.39     5.91    12.51
01-12-15    27.48   1505.03     0.56    25.25

最佳答案

使用 seq 以及 fromto 日期创建“MonYear”。

 MonYear <- format(seq(as.Date('2015-01-01'), as.Date('2015-12-01'),
              by = 'month'), '%d-%m-%y')

转置原始数据集中的非数字列(输出将是一个矩阵。我们通过组合“MonYear”和矩阵输出来创建一个data.frame .

 df2 <- data.frame(MonYear,t(df1[-1]))

相应地更改列名称和行名称

 colnames(df2)[-1] <- LETTERS[1:4]
 row.names(df2) <- NULL
  df2 
 MonYear     A       B     C     D
1  01-01-15 28.21 1333.00 10.05 18.22
2  01-02-15 42.03 1348.64  5.46 18.41
3  01-03-15 48.56 1364.28  4.82 14.31
4  01-04-15 46.85 1379.92  5.27 30.28
5  01-05-15 46.03 1395.56  5.07 18.16
6  01-06-15 54.60 1411.20  4.07 15.52
7  01-07-15 63.87 1426.84  9.53 12.52
8  01-08-15 50.00 1442.48  1.95 13.14
9  01-09-15 53.34 1458.11  6.95 15.05
10 01-10-15 43.47 1473.75  6.54  8.89
11 01-11-15 34.66 1489.39  5.91 12.51
12 01-12-15 27.48 1505.03  0.56 25.25

关于r - 在 Azure ML Studio 中转置和添加 R 中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31199429/

相关文章:

c++ - 使用 R 创建图形/绘图/图表并使用 GTK 显示它们并与之交互

azure - Azure 上 API 应用程序中的 Microsoft 帐户身份验证

python - 在 Apache Spark 中使用 pyspark 转置 Dataframe

matrix - CUDA中共享内存的非方矩阵转置

java - 如何在Hadoop集群中加载native-hadoop库?

css - 在 shinydashboard 上使用 CSS 创建的圆圈内不显示数字

azure - Virto Commerce Azure 部署连接尝试失败

azure - 在哪里可以下载适用于 Visual Studio 2010 的 Azure SDK?

awk - 使用 gawk 转置列和行

r - 将 R 中的数据帧连接/合并到向量类型单元中