1

I use RGoogleDocs a lot. I use it to read in data that is private or only shared with a few people. I know that read.table and read.csv allow one to use stringsAsFactors=FALSE.

I want to do something similar in RGoogleDocs. Here is my typical code

sheets.con <- getGoogleDocsConnection(getGoogleAuth("fjb@gmail.com", ps, service ="wise"))
spreadsheet <- getWorksheets("private spreadsheet",sheets.con)
first <- sheetAsMatrix(spreadsheet$"Sheet 1",header=TRUE, as.data.frame=TRUE, trim=TRUE) #Get one sheet

That almost always reads the character values as factors. I have been using data.table a lot lately and factors seem to make my data munging in data.table a bit bothersome. Is there an easy way to read the non-numeric columns as character vectors rather than as factors?

tmthydvnprt
  • 10,398
  • 8
  • 52
  • 72
Farrel
  • 10,244
  • 19
  • 61
  • 99
  • 1
    Why not add another line to fix this right after the read statement? – Maiasaura Oct 11 '12 at 02:23
  • And what exactly is the bother? – Matt Dowle Oct 11 '12 at 06:12
  • I sometimes use something like `dat[sapply(dat, is.factor)] = lapply(dat[sapply(dat, is.factor)], as.character)` on `data.frame`s where I want to convert all of the factors to characters. – A5C1D2H2I1M1N2O1R2T1 Oct 11 '12 at 06:57
  • @MatthewDowle somewhere in my memory I have vague recollections of issues with unused levels of factors when adding new rows. Factors work well when one is at the statistical analysis phase but I have often found it to be a pain at the data pre-processing stage. – Farrel Oct 11 '12 at 14:47
  • @Farrel I'm struggling to see what the question is. – Matt Dowle Oct 11 '12 at 15:32
  • @MatthewDowle. The question is "Is there a way to import Google Docs Spreadsheets into a data.table or into a data.frame such that columns with character values do not get converted to factors?" The RGoogleDocs package is to my knowledge the only currently working way to read straight from Google Docs into R. Maiasaura has recommended that I should simply add something such as that suggested by mrdwab above. I guess one line will not kill me. I was hoping that someone who was intimately familiar with RGoogleDocs package would add their 2c worth. – Farrel Oct 11 '12 at 21:33

1 Answers1

3

Duncan Temple Lang, the writer of RGoogleDocs updated the package to 0.7-0. He added stringsAsFactors to the getWorksheets() and sheetAsMatrix() function.

Here is how you can get it into your R.

   install.packages("devtools")
   library(devtools)
   install_github("RGoogleDocs", "duncantl")

Now my line is

   first <- sheetAsMatrix(spreadsheet$"Sheet 1",header=TRUE, as.data.frame=TRUE, trim=TRUE, stringsAsFactors=FALSE) #Get one sheet
Farrel
  • 10,244
  • 19
  • 61
  • 99