【问题标题】:How to write rownames into a spreadsheet with the googlesheets package in R?如何使用 R 中的 googlesheets 包将行名写入电子表格?
【发布时间】:2016-07-08 06:40:17
【问题描述】:

我想用 googlessheets 包在 Google 电子表格中编写一个数据框,但行名没有写在第一列中。

我的数据框如下所示:

> 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 没有使用行名创建列。仅使用它们的数据创建了两列(进攻和防守)。

我尝试将数据框转换为矩阵,但还是一样。

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

谢谢

【问题讨论】:

    标签: r r-googlesheets


    【解决方案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"
    

    从这里只需像以前一样将统计信息传递给 googlessheets 包中的函数

    【讨论】:

    • 谢谢,这正是我所需要的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    相关资源
    最近更新 更多