[R] comma delimiter & comma in text

Romain Francois rfrancois at mango-solutions.com
Wed Nov 29 10:58:19 CET 2006


Alexander Nervedi wrote:
> Hi
>
> I have data like
>
> 1, A, 24, The Red House
> 2, A, 25, King's Home, by the Sea
> ...
>
> I'd like to read this in as three variables. I first tried
>
> temp <-read.csv(addresses, sep = "," ) it worked but line 2 was broken 
> after King's Home, and by the Sea as placed in another line. and so i 
> eneded up with more number of rows than in the data. when i tried
> temp <-read.csv(addresses, sep = "," , flush = TRUE) i got things 
> right number of rows, but column 3 was truncated till the 3rd comma.
>
> Is there a way i can specify to R that "King's Home, by the Sea" is 
> one word?
>
> u r pal, al
Hi,

If you know that the "guilty" column will be the last one, you can 
always try to make up your own read function using readLines :

do.call( rbind,
   lapply( strsplit(readLines("data.txt"), "," ), function(x) {
      gsub("^[[:space:]]|[[:space:]]$", "",                            # 
just removing leading and trailing spaces
               c( head(x,3), paste(tail(x, -3), collapse=",")))
   } )
)


Cheers,

Romain

-- 
Mango Solutions
Tel  +44 1249 467 467
Fax  +44 1249 467 468
Mob  +44 7813 526 123
data analysis that delivers



More information about the R-help mailing list