如何使用R中的googlesheets包将rownames写入电子表格?

时间:2016-03-21 16:20:16

标签: r r-googlesheets

我想在Google电子表格中使用googlessheets包编写数据框,但是第一列中没有写入rownames。

我的数据框如下所示:

> str(stats)
'data.frame':   4 obs. of  2 variables:
 $ Offensive: num  194.7 87 62.3 10.6
 $ Defensive: num  396.28 51.87 19.55 9.19

> stats
                                 Offensive   Defensive
Annualized Return               194.784261  396.278385
Annualized Standard Deviation     87.04125   51.872826
Worst Drawdown                    22.26618    9.546208
Annualized Sharpe Ratio (Rf=0%)   1.61126    0.9193734

我按照文档中的建议加载库,创建电子表格&然后,工作表使用gs_edit_cells命令写入数据:

> install.packages("googlesheets")
> library("googlesheets")
> suppressPackageStartupMessages(library("dplyr"))

> mySpreadsheet <- gs_new("mySpreadsheet")
> mySpreadsheet <- mySpreadsheet %>% gs_ws_new("Stats")
> mySpreadsheet <- mySpreadsheet %>% gs_edit_cells(ws = "Stats", input = stats, trim = TRUE)

一切顺利但googlesheets没有创建带有rownames的列。只使用他们的数据创建了两列(攻击性和防御性)。

我尝试将数据框转换为矩阵但仍然相同。

知道如何实现这一目标吗?

谢谢

1 个答案:

答案 0 :(得分:1)

看起来gs_edit_cells()没有行名称参数。如果您只想在表格的第一列中显示行名称,可以尝试:

stats$Rnames = rownames(stats)                  ## add column equal to the row names
stats[,c("Rnames","Offensive", "Defensive")]    ## re order so names are first
# names(stats) = c("","Offensive", "Defensive") optional if you want the names col to not have a "name"

从这里开始,就像你之前一样将stats传递给googlessheets包中的函数