[newbie] read row from file into vector

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[newbie] read row from file into vector

Tom Roche

summary: how to read a row (not column) from a file into a vector (not a data frame)?

details:

I'm using

$ lsb_release -ds
Linux Mint Debian Edition
$ uname -rv
3.0.0-1-amd64 #1 SMP Sun Jul 24 02:24:44 UTC 2011
$ R --version
R version 2.14.1 (2011-12-22)

I'm new to R (having previously used it only for graphics), but have worked in many other languages. I've got a CSV file from which I'd like to read the values from a single *row* into a vector. E.g., for a file such that

$ head -n 2 ~/data/foo.csv | tail -n 1
5718,0.3,0.47,0,0,0,0,0,0,0,0,0.08,0.37,0,0,0.83,1.55,0,0,0,0,0,0,0,0,0,0.00,2.48,2.33,0.17,0,0,0,0,0,0,0.00,10.69,0.18,0,0,0,0

I'd like to be able to populate a vector 'v' s.t. v[1]=5718, ... v[43]=0

I can't seem to do that with, e.g., read.csv(...) or scan(...), both of which seem column-oriented. What am I missing?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [newbie] read row from file into vector

Rui Barradas
Tom Roche wrote
summary: how to read a row (not column) from a file into a vector (not a data frame)?

details:

I'm using

$ lsb_release -ds
Linux Mint Debian Edition
$ uname -rv
3.0.0-1-amd64 #1 SMP Sun Jul 24 02:24:44 UTC 2011
$ R --version
R version 2.14.1 (2011-12-22)

I'm new to R (having previously used it only for graphics), but have worked in many other languages. I've got a CSV file from which I'd like to read the values from a single *row* into a vector. E.g., for a file such that

$ head -n 2 ~/data/foo.csv | tail -n 1
5718,0.3,0.47,0,0,0,0,0,0,0,0,0.08,0.37,0,0,0.83,1.55,0,0,0,0,0,0,0,0,0,0.00,2.48,2.33,0.17,0,0,0,0,0,0,0.00,10.69,0.18,0,0,0,0

I'd like to be able to populate a vector 'v' s.t. v[1]=5718, ... v[43]=0

I can't seem to do that with, e.g., read.csv(...) or scan(...), both of which seem column-oriented. What am I missing?

______________________________________________
[hidden email] 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.
Hello,

?readLines

If you want the 2nd line it's fast, if you have a large file, cycle through.

Rui Barradas
Reply | Threaded
Open this post in threaded view
|

Re: [newbie] read row from file into vector

Duncan Murdoch-2
In reply to this post by Tom Roche
On 11-12-29 3:51 PM, Tom Roche wrote:

>
> summary: how to read a row (not column) from a file into a vector (not a data frame)?
>
> details:
>
> I'm using
>
> $ lsb_release -ds
> Linux Mint Debian Edition
> $ uname -rv
> 3.0.0-1-amd64 #1 SMP Sun Jul 24 02:24:44 UTC 2011
> $ R --version
> R version 2.14.1 (2011-12-22)
>
> I'm new to R (having previously used it only for graphics), but have worked in many other languages. I've got a CSV file from which I'd like to read the values from a single *row* into a vector. E.g., for a file such that
>
> $ head -n 2 ~/data/foo.csv | tail -n 1
> 5718,0.3,0.47,0,0,0,0,0,0,0,0,0.08,0.37,0,0,0.83,1.55,0,0,0,0,0,0,0,0,0,0.00,2.48,2.33,0.17,0,0,0,0,0,0,0.00,10.69,0.18,0,0,0,0
>
> I'd like to be able to populate a vector 'v' s.t. v[1]=5718, ... v[43]=0
>
> I can't seem to do that with, e.g., read.csv(...) or scan(...), both of which seem column-oriented. What am I missing?

Those are both column oriented, but you can change the result to a
vector after reading it.

For example,

x <- read.csv("foo.csv", nrow=1)
x <- as.numeric(x[1,])  # convert to numeric vector

If you don't want the first row, use skip=<something> in the read.csv
call.

Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [newbie] read row from file into vector

William Dunlap
In reply to this post by Tom Roche
Look into connection objects, which let you open a file or other
readable sort of thing and read it piece by piece.  E.g., the
following function opens your file (making a "file connection"),
skips some lines, reads the desired line into a character object,
then reads from that character object (as a "text connection")
to make a numeric object:

f <- function (fileName, lineNumber, ...)
{
    connection <- file(fileName, "rt")
    on.exit(close(connection))
    if (lineNumber > 1) {
        readLines(connection, n = lineNumber - 1)
    }
    lineText <- readLines(connection, n = 1)
    scan(textConnection(lineText), ...)
}

Here is a self-contained example:

> tfile <- tempfile()
> cat(file=tfile, sep="\n", "A data file with a header line",
+     "101;102;103", "104,105", "106/107/108")
> cat(readLines(tfile), sep="\n") # look at text in file
A data file with a header line
101;102;103
104,105
106/107/108
> f(tfile, lineNumber=3, sep=",") # read third line as comma separated numbers
Read 2 items
[1] 104 105

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of Tom Roche
> Sent: Thursday, December 29, 2011 12:51 PM
> To: [hidden email]
> Subject: [R] [newbie] read row from file into vector
>
>
> summary: how to read a row (not column) from a file into a vector (not a data frame)?
>
> details:
>
> I'm using
>
> $ lsb_release -ds
> Linux Mint Debian Edition
> $ uname -rv
> 3.0.0-1-amd64 #1 SMP Sun Jul 24 02:24:44 UTC 2011
> $ R --version
> R version 2.14.1 (2011-12-22)
>
> I'm new to R (having previously used it only for graphics), but have worked in many other languages.
> I've got a CSV file from which I'd like to read the values from a single *row* into a vector. E.g.,
> for a file such that
>
> $ head -n 2 ~/data/foo.csv | tail -n 1
> 5718,0.3,0.47,0,0,0,0,0,0,0,0,0.08,0.37,0,0,0.83,1.55,0,0,0,0,0,0,0,0,0,0.00,2.48,2.33,0.17,0,0,0,0,0,
> 0,0.00,10.69,0.18,0,0,0,0
>
> I'd like to be able to populate a vector 'v' s.t. v[1]=5718, ... v[43]=0
>
> I can't seem to do that with, e.g., read.csv(...) or scan(...), both of which seem column-oriented.
> What am I missing?
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [newbie] read row from file into vector

Greg Snow-2
In reply to this post by Tom Roche
The scan function can be used to read a single row.  If your file has multiple rows you can use the skip and nlines arguments to determine which row to read.  With the what argument sent to a single item (a number or string depending on which you want) it will read each element on that row into a vector.

If you want to do more of the hard work yourself you can read in a whole line as a single string using the readLines function then use the strsplit (or possibly better, tools from the gsubfun package) to split that string into a vector (the unlist function may also be of help).

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Tom Roche
> Sent: Thursday, December 29, 2011 1:51 PM
> To: [hidden email]
> Subject: [R] [newbie] read row from file into vector
>
>
> summary: how to read a row (not column) from a file into a vector (not
> a data frame)?
>
> details:
>
> I'm using
>
> $ lsb_release -ds
> Linux Mint Debian Edition
> $ uname -rv
> 3.0.0-1-amd64 #1 SMP Sun Jul 24 02:24:44 UTC 2011
> $ R --version
> R version 2.14.1 (2011-12-22)
>
> I'm new to R (having previously used it only for graphics), but have
> worked in many other languages. I've got a CSV file from which I'd like
> to read the values from a single *row* into a vector. E.g., for a file
> such that
>
> $ head -n 2 ~/data/foo.csv | tail -n 1
> 5718,0.3,0.47,0,0,0,0,0,0,0,0,0.08,0.37,0,0,0.83,1.55,0,0,0,0,0,0,0,0,0
> ,0.00,2.48,2.33,0.17,0,0,0,0,0,0,0.00,10.69,0.18,0,0,0,0
>
> I'd like to be able to populate a vector 'v' s.t. v[1]=5718, ...
> v[43]=0
>
> I can't seem to do that with, e.g., read.csv(...) or scan(...), both of
> which seem column-oriented. What am I missing?
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [newbie] read row from file into vector

Tom Roche
In reply to this post by William Dunlap

Tom Roche 11-12-29 3:51 PM
>>> E.g., for a file such that

>>> $ head -n 2 ~/data/foo.csv | tail -n 1
>>> 5718,0.3,0.47,0,0,0,0,0,0,0,0,0.08,0.37,0,0,0.83,1.55,0,0,0,0,0,0,0,0,0,0.00,2.48,2.33,0.17,0,0,0,0,0,0,0.00,10.69,0.18,0,0,0,0

>>> I'd like to be able to populate a vector 'v' s.t.
>>> v[1]=5718, ... v[43]=0

Duncan Murdoch Thu, 29 Dec 2011 16:45:45 -0500
>> x <- read.csv("foo.csv", nrow=1)
>> x <- as.numeric(x[1,])  # convert to numeric vector

Aha!

William Dunlap Thu, 29 Dec 2011 21:49:13 +0000
> Look into connection objects, which let you open a file or other
> readable sort of thing and read it piece by piece.

Will do, since what I plan to use R for (mostly) is manipulating very large netCDF files.

thanks, all! Tom Roche <[hidden email]>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [newbie] read row from file into vector

Daniel Nordlund-4
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of Tom Roche
> Sent: Thursday, December 29, 2011 2:08 PM
> To: [hidden email]
> Subject: Re: [R] [newbie] read row from file into vector
>
>
> Tom Roche 11-12-29 3:51 PM
> >>> E.g., for a file such that
>
> >>> $ head -n 2 ~/data/foo.csv | tail -n 1
> >>>
> 5718,0.3,0.47,0,0,0,0,0,0,0,0,0.08,0.37,0,0,0.83,1.55,0,0,0,0,0,0,0,0,0,0.
> 00,2.48,2.33,0.17,0,0,0,0,0,0,0.00,10.69,0.18,0,0,0,0
>
> >>> I'd like to be able to populate a vector 'v' s.t.
> >>> v[1]=5718, ... v[43]=0
>
> Duncan Murdoch Thu, 29 Dec 2011 16:45:45 -0500
> >> x <- read.csv("foo.csv", nrow=1)
> >> x <- as.numeric(x[1,])  # convert to numeric vector
>
> Aha!
>
> William Dunlap Thu, 29 Dec 2011 21:49:13 +0000
> > Look into connection objects, which let you open a file or other
> > readable sort of thing and read it piece by piece.
>
> Will do, since what I plan to use R for (mostly) is manipulating very
> large netCDF files.
>
> thanks, all! Tom Roche <[hidden email]>
>

Tom,

you might also want to see what is already available in R for handling netCDF files.  Install the sos package (if you haven't already) and run the command

findFn('netCDF')

It returned quite a lot of functions for dealing with netCDF  files/data.


Hope this is helpful,

Dan

Daniel Nordlund
Bothell, WA USA

______________________________________________
[hidden email] 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.