[R] Importing data with different delimters

jim holtman jholtman at gmail.com
Sat Jun 14 01:28:57 CEST 2008


Assuming that the only problem is the blank in the city names, here is
one way of doing it:

> inFile <- textConnection("City State      JanTemp Lat     Long
+ Mobile, AL      44      31.2    88.5
+ Montgomery, AL  38      32.9    86.8
+ Phoenix, AZ     35      33.6    112.5
+ Little Rock, AR 31      35.4    92.8
+ Los Angeles, CA 47      34.3    118.7
+ San Francisco, CA       42      38.4    123.0")
> lines <- readLines(inFile)
> # get rid of blanks in city names
> newLines <- sub("(.*?) +(.*),", "\\1_\\2,", lines)
>
> x <- read.table(textConnection(newLines), header=TRUE)
> closeAllConnections()
> x
            City State JanTemp  Lat  Long
1        Mobile,    AL      44 31.2  88.5
2    Montgomery,    AL      38 32.9  86.8
3       Phoenix,    AZ      35 33.6 112.5
4   Little_Rock,    AR      31 35.4  92.8
5   Los_Angeles,    CA      47 34.3 118.7
6 San_Francisco,    CA      42 38.4 123.0
>
>

If you want, you can then go back and replace the "_" with a blank in
the city name.

On Fri, Jun 13, 2008 at 7:14 PM, David Arnold <dwarnold45 at suddenlink.net> wrote:
> All,
>
> I have a data file with 56 entries that looks like this:
>
> City State      JanTemp Lat     Long
> Mobile, AL      44      31.2    88.5
> Montgomery, AL  38      32.9    86.8
> Phoenix, AZ     35      33.6    112.5
> Little Rock, AR 31      35.4    92.8
> Los Angeles, CA 47      34.3    118.7
> San Francisco, CA       42      38.4    123.0
>
> I would like to "read" this data into a dataframe. Is it possible to do
> without editing the datafile?
>
> D.
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list