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

  • Home
  • SEARCH
  • 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 6164629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:01:38+00:00 2026-05-23T22:01:38+00:00

R 2.13.1 on Mac OS X. I’m trying to import a data file that

  • 0

R 2.13.1 on Mac OS X. I’m trying to import a data file that has a point for thousand separator and comma as the decimal point, as well as trailing minus for negative values.

Basically, I’m trying to convert from:

"A|324,80|1.324,80|35,80-"

to

  V1    V2     V3    V4
1  A 324.80 1324.8 -35.80

Now, interactively both the following works:

gsub("\\.","","1.324,80")
[1] "1324,80"

gsub("(.+)-$","-\\1", "35,80-")
[1] "-35,80"

and also combining them:

gsub("\\.", "", gsub("(.+)-$","-\\1","1.324,80-"))
[1] "-1324,80"

However, I’m not able to remove the thousand separator from read.data:

setClass("num.with.commas")

setAs("character", "num.with.commas", function(from) as.numeric(gsub("\\.", "", sub("(.+)-$","-\\1",from))) )
mydata <- "A|324,80|1.324,80|35,80-"

mytable <- read.table(textConnection(mydata), header=FALSE, quote="", comment.char="", sep="|", dec=",", skip=0, fill=FALSE,strip.white=TRUE, colClasses=c("character","num.with.commas", "num.with.commas", "num.with.commas"))

Warning messages:
1: In asMethod(object) : NAs introduced by coercion
2: In asMethod(object) : NAs introduced by coercion
3: In asMethod(object) : NAs introduced by coercion

mytable
  V1 V2 V3 V4
1  A NA NA NA

Note that if I change from “\\.” to “,” in the function, things look a bit different:

setAs("character", "num.with.commas", function(from) as.numeric(gsub(",", "", sub("(.+)-$","-\\1",from))) )

mytable <- read.table(textConnection(mydata), header=FALSE, quote="", comment.char="", sep="|", dec=",", skip=0, fill=FALSE,strip.white=TRUE, colClasses=c("character","num.with.commas", "num.with.commas", "num.with.commas"))

mytable
  V1    V2     V3    V4
1  A 32480 1.3248 -3580

I think the problem is that read.data with dec=”,” converts the incoming “,” to “.” BEFORE calling as(from, “num.with.commas”), so that the input string can be e.g. “1.324.80”.

I want as(“1.123,80-“,”num.with.commas”) to return -1123.80 and as(“1.100.123,80”, “num.with.commas”) to return 1100123.80.

How can I make my num.with.commas replace all except the last decimal point in the input string?

Update: First, I added negative lookahead and got as() working in the console:

setAs("character", "num.with.commas", function(from) as.numeric(gsub("(?!\\.\\d\\d$)\\.", "", gsub("(.+)-$","-\\1",from), perl=TRUE)) )
as("1.210.123.80-","num.with.commas")
[1] -1210124
as("10.123.80-","num.with.commas")
[1] -10123.8
as("10.123.80","num.with.commas")
[1] 10123.8

However, read.table still had the same problem. Adding some print()s to my function showed that num.with.commas in fact got the comma and not the point.

So my current solution is to then replace from “,” to “.” in num.with.commas.

setAs("character", "num.with.commas", function(from) as.numeric(gsub(",","\\.",gsub("(?!\\.\\d\\d$)\\.", "", gsub("(.+)-$","-\\1",from), perl=TRUE))) )
mytable <- read.table(textConnection(mydata), header=FALSE, quote="", comment.char="", sep="|", dec=",", skip=0, fill=FALSE,strip.white=TRUE, colClasses=c("character","num.with.commas", "num.with.commas", "num.with.commas"))
mytable
  V1    V2      V3    V4
1  A 324.8 1101325 -35.8
  • 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-23T22:01:39+00:00Added an answer on May 23, 2026 at 10:01 pm

    You should be removing all the periods first and then changing the commas to decimal points before coercing with as.numeric(). You can later control how decimal points are printed with options(OutDec=”,”) . I do not think R uses commas as decimal separators internally even in locales where they are conventional.

    > tst <- c("A","324,80","1.324,80","35,80-")
    > 
    > as.numeric( sub("\\,", ".", sub("(.+)-$","-\\1", gsub("\\.", "", tst)) ) )
    [1]     NA  324.8 1324.8  -35.8
    Warning message:
    NAs introduced by coercion 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Mac OS X API, I'm trying to save a PDF file with a
Mac OSX Finder has a search feature that darkens everything but the part that
I'm on Mac Leopard. Trying to get django admin file uploads, or really image
On mac os x, I'm trying to link against frameworks that are in a
With Mac OS X 10.7 Apple has introduced AVFoundation and I'm trying to generate
My mac has folders Shared and Public folders. Their permissions are drwxr-xr-x+ for Public
The Mac build of my (mainly POSIX) application spawns a child thread that calls
(Mac)Vim seems to be picking up /usr/bin/python instead of the one that's at the
Mac OS X 10.6 (Snow Leopard) has VIM pre-installed (version 7.2), which is great.
In Mac Excel 2011 I have a Scrum burndown chart that tracks the number

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.