[R] comma delimiter & comma in tex

Gabor Grothendieck ggrothendieck at gmail.com
Wed Nov 29 15:14:57 CET 2006


Preprocess the file surrounding the last field with quotes.  The
xx <- line is for purposes of making it self contained here and in
reality would
be replaced with the commented
line above it.  It simply reads the text in.

The yy <- line
then surrounds the fourth field with double quotes assuming
at least a space separates the fields as in your example.

Finally read.table reads it now that the fourth field
has been protected with quotes and closeAllConnections
closes the dangling connections.

# test data
Lines <- "1, A, 24, The Red House
2, A, 25, King's Home, by the Sea
"

# xx <- readLines("myfile.dat")
xx <- readLines(textConnection(Lines))
yy <- gsub('^( *[^ ]+ [^ ]+ [^ ]+ )(.*)', '\\1"\\2"', xx)
read.table(textConnection(yy), sep = ",")
closeAllConnections()



On 11/29/06, Alexander Nervedi <alexnerdy at hotmail.com> 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
>
> _________________________________________________________________
> All-in-one security and maintenance for your PC. Get a free 90-day trial!
>
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>



More information about the R-help mailing list