矩阵转DF转置R

时间:2018-08-28 14:00:39

标签: r dataframe

我有一个要转置到数据帧的矩阵。当我这样做时,第一栏没有名称。我怎么命名。下面是矩阵和转置的df。

Metric_name   May 2017 Jun 2017 Jul 2017 Aug 2017 Sep 2017
[1,] "nike"   "12"     "38"     "41"     "34"     "45"    
[2,] "adidas" "643"    "673"    "585"    "580"    "662"   
[3,] "UA"     "442"    "502"    "634"    "667"    "804"   
[4,] "Puma"   "1145"   "1218"   "1342"   "1329"   "1273" 

代码

dj1<- as.data.frame(t(dj()))
dj2<- dj1() %>% set_names(unlist(.[1,])) %>% tail(-1)

输出df:

              nike adidas   UA    Puma
May 2017      12    643    442   1145
Jun 2017      38    673    502   1218
Jul 2017      41    585    634   1342
Aug 2017      34    580    667   1329
Sep 2017      45    662    804   1273

0 个答案:

没有答案