Read data from .csv file as a matrix

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

Read data from .csv file as a matrix

vincent.deluard
Hi R-users,
 

I have a csv file that contains weather observation (rows) by days (in columns).

I open using:

> temp = read.csv("Weather.csv", sep=",")

and read:

             X X1.Jan X2.Jan X3.Jan X4.Jan
1          Min      2      3      4      1
2          Max      6     10      8      6
3 Forecast Min      3      1      1      3
4 Forecast Max      8      7      4      9

If I type

mean(temp[2,2:3])

I get

X1.Jan X2.Jan
     6     10

The same command on

> y = matrix(1:21, ncol=7)

> mean(y[2,2:3])
[1] 6.5

Works because the data is in a matrix. I believe R stores the data from the csv file as a data.frame with these annoying headers. So how do I convert the data from my csv file into a matrix?

I tried as.matrix but it did not help.


Many many thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Read data from .csv file as a matrix

Erik Iverson-3
Hello,

> I have a csv file that contains weather observation (rows) by days (in
> columns).
>
> I open using:
>
>> temp = read.csv("Weather.csv", sep=",")
>
> and read:
>
>              X X1.Jan X2.Jan X3.Jan X4.Jan
> 1          Min      2      3      4      1
> 2          Max      6     10      8      6
> 3 Forecast Min      3      1      1      3
> 4 Forecast Max      8      7      4      9
>
> If I type
>
> mean(temp[2,2:3])
>
> I get
>
> X1.Jan X2.Jan
>      6     10
>
> The same command on
>
>> y = matrix(1:21, ncol=7)
>
>> mean(y[2,2:3])
> [1] 6.5
>
> Works because the data is in a matrix. I believe R stores the data from the
> csv file as a data.frame with these annoying headers. So how do I convert
> the data from my csv file into a matrix?
>
> I tried as.matrix but it did not help.
>

Giving us a reproducible example with ?dput output from your temp object
would most likely help us solve the problem very quickly.

______________________________________________
[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: Read data from .csv file as a matrix

Nordlund, Dan (DSHS/RDA)
In reply to this post by vincent.deluard
> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of vincent.deluard
> Sent: Wednesday, May 05, 2010 12:34 PM
> To: [hidden email]
> Subject: [R] Read data from .csv file as a matrix
>
>
> Hi R-users,
>
>
> I have a csv file that contains weather observation (rows) by days (in
> columns).
>
> I open using:
>
> > temp = read.csv("Weather.csv", sep=",")
>
> and read:
>
>              X X1.Jan X2.Jan X3.Jan X4.Jan
> 1          Min      2      3      4      1
> 2          Max      6     10      8      6
> 3 Forecast Min      3      1      1      3
> 4 Forecast Max      8      7      4      9
>
> If I type
>
> mean(temp[2,2:3])
>
> I get
>
> X1.Jan X2.Jan
>      6     10
>
> The same command on
>
> > y = matrix(1:21, ncol=7)
>
> > mean(y[2,2:3])
> [1] 6.5
>
> Works because the data is in a matrix. I believe R stores the data from
> the
> csv file as a data.frame with these annoying headers. So how do I
> convert
> the data from my csv file into a matrix?
>
> I tried as.matrix but it did not help.
>
>
> Many many thanks!

I'm only guessing because you haven't given us much to go on, but maybe something like

mean(unlist(temp[2,2:3]))

Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


______________________________________________
[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: Read data from .csv file as a matrix

Tao Shi
In reply to this post by vincent.deluard

Vincent,

The root of this problem seems to be that you don't fully understand the differences between matrix and data.frame.  Read up on them and you'll know how to solve this problem.

For now:

as.matrix(temp[,-1])

or
temp = read.csv("Weather.csv", sep=",", row.names=1)
temp1 <- as.matrix(temp)

should work.


...Tao






> I have a csv file that contains weather observation (rows) by days (in
> columns).
>
> I open using:
>
>> temp = read.csv("Weather.csv", sep=",")
>
> and read:
>
>              X X1.Jan X2.Jan X3.Jan X4.Jan
> 1          Min      2      3      4      1
> 2          Max      6     10      8      6
> 3 Forecast Min      3      1      1      3
> 4 Forecast Max      8      7      4      9
>
> If I type
>
> mean(temp[2,2:3])
>
> I get
>
> X1.Jan X2.Jan
>      6     10
>
> The same command on
>
>> y = matrix(1:21, ncol=7)
>
>> mean(y[2,2:3])
> [1] 6.5
>
> Works because the data is in a matrix. I believe R stores the data from the
> csv file as a data.frame with these annoying headers. So how do I convert
> the data from my csv file into a matrix?
>
> I tried as.matrix but it did not help.
>

     
_________________________________________________________________
The New Busy is not the old busy. Search, chat and e-mail from your inbox.

N:WL:en-US:WM_HMP:042010_3
______________________________________________
[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.