Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6974997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:18:39+00:00 2026-05-27T17:18:39+00:00

I am new to R and am trying to read a public Google spreadsheet

  • 0

I am new to R and am trying to read a public Google spreadsheet into an R data frame with numeric columns. My problem seems to be that the exported spreadsheet has commas in large numbers, such as “13,061.422”. The read.csv() function treats this as a factor. I tried stringsAsFactors=FALSE and colClasses=c(rep(“numeric”,7)) but neither worked. Is there a way to coerce the values with commas and decimals to numeric values, either within read.csv() or afterwards when they are treated as Factors in the R dataframe? Here is my code:

require(RCurl)

myCsv <- getURL("https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0Agbdciapt4QZdE95UDFoNHlyNnl6aGlqbGF0cDIzTlE&single=true&gid=0&range=A1%3AG4928&output=csv", ssl.verifypeer=FALSE)  #ssl.verifypeer=FALSE gets around certificate issues I don't understand.

fullmatrix <- read.csv(textConnection(myCsv))

str(fullmatrix)

which results in:

'data.frame':   4927 obs. of  7 variables:
 $ wave.      : Factor w/ 4927 levels "1,000.8900","1,002.8190",..: 4875 4874 4873 4872 4871 4870 4869 4868 4867 4866 ...
 $ wavelength : Factor w/ 4927 levels "1,000.074","1,000.267",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ d2o        : num  85.2 87.7 86.3 87.6 85.6 ...
 $ di         : num  54.3 55.8 54.9 55.6 54.9 ...
 $ ddw        : num  48.2 49.7 49.4 50.2 49.6 ...
 $ ddw.old    : num  53.3 55 53.9 54.8 53.7 ...
 $ d2o.ddw.mix: num  65.8 67.9 67.2 68.4 66.8 ...

Thanks for any help! I am new to R, so guessing (hoping) this is an easy one!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:18:40+00:00Added an answer on May 27, 2026 at 5:18 pm

    Yes. Two methods. The easiest to understand at first is probably just to is as.is=TRUE to preserve them as character vectors and then use gsub to remove the commas and any currency symbols before converting to numeric. The second is a bit more difficult, but I think more kewl. Create an as-method for the format you are using. Then you can use colClasses to do it in one step.

    I see @EDi already did version #1 (using stringsAsFactors rather than as.is, so I will document strategy #2:

     library(methods)
     setClass("num.with.commas")
    #[1] "num.with.commas"
     setAs("character", "num.with.commas",
          function(from) as.numeric(gsub(",", "", from)))
     require(RCurl)
    #Loading required package: RCurl
    #Loading required package: bitops
    
     myCsv <- getURL("https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0Agbdciapt4QZdE95UDFoNHlyNnl6aGlqbGF0cDIzTlE&single=true&gid=0&range=A1%3AG4928&output=csv", ssl.verifypeer=FALSE)  
    > fullmatrix <- read.csv(textConnection(myCsv), 
                           colClasses=c(rep("num.with.commas",2), rep("numeric",4) ))
     str(fullmatrix)
    #--------------
    'data.frame':   4927 obs. of  7 variables:
     $ wave.      : num  9999 9997 9995 9993 9992 ...
     $ wavelength : num  1000 1000 1000 1001 1001 ...
     $ d2o        : num  85.2 87.7 86.3 87.6 85.6 ...
     $ di         : num  54.3 55.8 54.9 55.6 54.9 ...
     $ ddw        : num  48.2 49.7 49.4 50.2 49.6 ...
     $ ddw.old    : num  53.3 55 53.9 54.8 53.7 ...
     $ d2o.ddw.mix: num  65.8 67.9 67.2 68.4 66.8 ...
    

    as-methods are coercive. There are many such methods in base R, such as as.list, as.numeric, as.character. In each case they attempt to take input that is in one mode and make a sensible copy of that in a different mode. For instance, it makes sense to coerce a matrix to a dataframe because they both have two dimensions. It makes a bit less sense to coerce a dataframe to a matrix (but it does succeed with loss of all the attributes of the columns and coercion to a common mode.)

    In the present case I am taking a character string as input, removing any commas, and coercing the character values to numeric. Then I use read.table‘s ( in this case by way of read.csv) ‘colClasses’ argument to dispatch to the as-method I registered with setAs. You may want to go to the help(setAs) page for more details. The S4 class system confuses a lot of people, me included. This is about the only area of success I have had with S4 methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to CSS and I'm trying to read the code that comes with
I am new at shell scripting and I am trying to read data from
I am trying to read a file of multiple data types into an ArrayList
I'm trying to read XML data from Google weather webservice. The response contain some
Im trying to read data from a text file and loading it into a
I am trying to read data from the given web page using Java. public
I'm trying to read data from socket using threading run method: @Override public void
I am trying to read a file with doubles. I locate that problem occurs
I'm trying to read a log file of log4net: FileStream fs = new FileStream(filePath,
I am kind of new to Groovy and I am trying to read a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.