read.table or read.csv without row index?

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

read.table or read.csv without row index?

tsunhin wong
Hello all,

Probably my concepts about the data.frame and matrix and array in R
are not clear, I need some clarification to help me understand them
better.

>M <- read.table("test1.csv",sep=",",row.names=NULL,header=T)

gives me: M as

  M1 M2 M3 M4 M5 M6 M7 M8 M9 M10
1  9 11 14 15 18 20 20 20 20  20
2  3  4  8  9 11 12 14 15 15  15
3  4  5  8  8  9  9  9  9  9   9
4  4  5  7  8  8  8  8  8  8   9

1. How can I read the csv file to:

  M1 M2 M3 M4 M5 M6 M7 M8 M9 M10
[1,]  9 11 14 15 18 20 20 20 20  20
[2,]  3  4  8  9 11 12 14 15 15  15
[3,]  4  5  8  8  9  9  9  9  9   9
[4,]  4  5  7  8  8  8  8  8  8   9

2. or how can convert the above M to a format with [1,],[2,] etc
instead of 1,2,etc?

3. How can I read a text file so that I can get:
  [,1] [,2] [,3] [,4] [,5]
[1,]  9 11 14 15 18
[2,]  3  4  8  9 11
[3,]  4  5  8  8  9
[4,]  4  5  7  8  8

(instead of having the columns names V1 to V5?)

Thank you for your help!

Regards,

      John

______________________________________________
[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.table or read.csv without row index?

Henrique Dallazuanna
Try this:

unname(as.matrix(read.table('your_file.txt')))

On Wed, Apr 22, 2009 at 11:00 AM, tsunhin wong <[hidden email]> wrote:

> Hello all,
>
> Probably my concepts about the data.frame and matrix and array in R
> are not clear, I need some clarification to help me understand them
> better.
>
> >M <- read.table("test1.csv",sep=",",row.names=NULL,header=T)
>
> gives me: M as
>
>  M1 M2 M3 M4 M5 M6 M7 M8 M9 M10
> 1  9 11 14 15 18 20 20 20 20  20
> 2  3  4  8  9 11 12 14 15 15  15
> 3  4  5  8  8  9  9  9  9  9   9
> 4  4  5  7  8  8  8  8  8  8   9
>
> 1. How can I read the csv file to:
>
>  M1 M2 M3 M4 M5 M6 M7 M8 M9 M10
> [1,]  9 11 14 15 18 20 20 20 20  20
> [2,]  3  4  8  9 11 12 14 15 15  15
> [3,]  4  5  8  8  9  9  9  9  9   9
> [4,]  4  5  7  8  8  8  8  8  8   9
>
> 2. or how can convert the above M to a format with [1,],[2,] etc
> instead of 1,2,etc?
>
> 3. How can I read a text file so that I can get:
>  [,1] [,2] [,3] [,4] [,5]
> [1,]  9 11 14 15 18
> [2,]  3  4  8  9 11
> [3,]  4  5  8  8  9
> [4,]  4  5  7  8  8
>
> (instead of having the columns names V1 to V5?)
>
> Thank you for your help!
>
> Regards,
>
>      John
>
> ______________________________________________
> [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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[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.table or read.csv without row index?

Sarah Goslee
In reply to this post by tsunhin wong
Really, this depends on what you are trying to do. What's the underlying
problem you are solving? You can save a data frame to a file without
the names, if that's the real question, but I can't think of any reason to
not want names within R.

A matrix does not have to have row and column names, but a dataframe
is required to have them. If none are specified, sequential values will be
assigned.

> matrix(1:9, 3, 3)
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> data.frame(matrix(1:9, 3, 3))
  X1 X2 X3
1  1  4  7
2  2  5  8
3  3  6  9


Sarah


On Wed, Apr 22, 2009 at 10:00 AM, tsunhin wong <[hidden email]> wrote:

> Hello all,
>
> Probably my concepts about the data.frame and matrix and array in R
> are not clear, I need some clarification to help me understand them
> better.
>
>>M <- read.table("test1.csv",sep=",",row.names=NULL,header=T)
>
> gives me: M as
>
>  M1 M2 M3 M4 M5 M6 M7 M8 M9 M10
> 1  9 11 14 15 18 20 20 20 20  20
> 2  3  4  8  9 11 12 14 15 15  15
> 3  4  5  8  8  9  9  9  9  9   9
> 4  4  5  7  8  8  8  8  8  8   9
>
> 1. How can I read the csv file to:
>
>  M1 M2 M3 M4 M5 M6 M7 M8 M9 M10
> [1,]  9 11 14 15 18 20 20 20 20  20
> [2,]  3  4  8  9 11 12 14 15 15  15
> [3,]  4  5  8  8  9  9  9  9  9   9
> [4,]  4  5  7  8  8  8  8  8  8   9
>
> 2. or how can convert the above M to a format with [1,],[2,] etc
> instead of 1,2,etc?
>
> 3. How can I read a text file so that I can get:
>  [,1] [,2] [,3] [,4] [,5]
> [1,]  9 11 14 15 18
> [2,]  3  4  8  9 11
> [3,]  4  5  8  8  9
> [4,]  4  5  7  8  8
>
> (instead of having the columns names V1 to V5?)
>
> Thank you for your help!
>
> Regards,
>
>      John
>


--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[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.table or read.csv without row index?

jholtman
In reply to this post by tsunhin wong
You need to convert to a matrix and remove names:

> x <- read.table(textConnection("M1 M2 M3 M4 M5 M6 M7 M8 M9 M10
+ 1  9 11 14 15 18 20 20 20 20  20
+ 2  3  4  8  9 11 12 14 15 15  15
+ 3  4  5  8  8  9  9  9  9  9   9
+ 4  4  5  7  8  8  8  8  8  8   9"), header=TRUE)
> closeAllConnections()
> y <- as.matrix(x)
> x
  M1 M2 M3 M4 M5 M6 M7 M8 M9 M10
1  9 11 14 15 18 20 20 20 20  20
2  3  4  8  9 11 12 14 15 15  15
3  4  5  8  8  9  9  9  9  9   9
4  4  5  7  8  8  8  8  8  8   9
> y
  M1 M2 M3 M4 M5 M6 M7 M8 M9 M10
1  9 11 14 15 18 20 20 20 20  20
2  3  4  8  9 11 12 14 15 15  15
3  4  5  8  8  9  9  9  9  9   9
4  4  5  7  8  8  8  8  8  8   9
> dimnames(y) <- NULL
> y
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    9   11   14   15   18   20   20   20   20    20
[2,]    3    4    8    9   11   12   14   15   15    15
[3,]    4    5    8    8    9    9    9    9    9     9
[4,]    4    5    7    8    8    8    8    8    8     9
>


On Wed, Apr 22, 2009 at 10:00 AM, tsunhin wong <[hidden email]> wrote:

> Hello all,
>
> Probably my concepts about the data.frame and matrix and array in R
> are not clear, I need some clarification to help me understand them
> better.
>
>>M <- read.table("test1.csv",sep=",",row.names=NULL,header=T)
>
> gives me: M as
>
>  M1 M2 M3 M4 M5 M6 M7 M8 M9 M10
> 1  9 11 14 15 18 20 20 20 20  20
> 2  3  4  8  9 11 12 14 15 15  15
> 3  4  5  8  8  9  9  9  9  9   9
> 4  4  5  7  8  8  8  8  8  8   9
>
> 1. How can I read the csv file to:
>
>  M1 M2 M3 M4 M5 M6 M7 M8 M9 M10
> [1,]  9 11 14 15 18 20 20 20 20  20
> [2,]  3  4  8  9 11 12 14 15 15  15
> [3,]  4  5  8  8  9  9  9  9  9   9
> [4,]  4  5  7  8  8  8  8  8  8   9
>
> 2. or how can convert the above M to a format with [1,],[2,] etc
> instead of 1,2,etc?
>
> 3. How can I read a text file so that I can get:
>  [,1] [,2] [,3] [,4] [,5]
> [1,]  9 11 14 15 18
> [2,]  3  4  8  9 11
> [3,]  4  5  8  8  9
> [4,]  4  5  7  8  8
>
> (instead of having the columns names V1 to V5?)
>
> Thank you for your help!
>
> Regards,
>
>      John
>
> ______________________________________________
> [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.
>



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

What is the problem that you are trying to solve?

______________________________________________
[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.table or read.csv without row index?

vincent.deluard
In reply to this post by tsunhin wong
Hi,

I have the same problem as Wong.

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. 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.table or read.csv without row index?

JiHO-2
> I tried as.matrix but it did not help.

as.matrix() won't work because a matrix requires everything in it to
be of the same type (number, character, logical etc.). You do not have
only numbers in your data.frame, so it will convert everything to
character strings. If you try as.matrix(temp[,-1]) it should work
(assuming you only have characters in the first column, otherwise
remove all non-numeric columns).

But what you really want is to circumvent the fact that, on a
data.frame, mean works column-wise. In fact, when you call mean() on a
data.frame() it calls mean.data.frame(), which code you can see by
typing its name at the prompt:

    > mean.data.frame
    function (x, ...)
    sapply(x, mean, ...)
    <environment: namespace:base>

And indeed, it uses sapply() to apply the function mean to each
column. You could have tried:

    mean.default(temp[2,2:3])

but this returns and error because it needs a numeric vector and does
not automatically convert your data.frame into it. So you need:

    mean.default(as.numeric(temp[2,2:3]))

or more simply

    mean(as.numeric(temp[2,2:3]))

I hope that helped. Sincerely,

JiHO
---
http://maururu.net

______________________________________________
[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.table or read.csv without row index?

David Winsemius

On May 5, 2010, at 3:49 PM, JiHO wrote:

>> I tried as.matrix but it did not help.
>
> as.matrix() won't work because a matrix requires everything in it to
> be of the same type (number, character, logical etc.). You do not have
> only numbers in your data.frame, so it will convert everything to
> character strings. If you try as.matrix(temp[,-1]) it should work
> (assuming you only have characters in the first column, otherwise
> remove all non-numeric columns).
>
> But what you really want is to circumvent the fact that, on a
> data.frame, mean works column-wise. In fact, when you call mean() on a
> data.frame() it calls mean.data.frame(), which code you can see by
> typing its name at the prompt:
>
>> mean.data.frame
>    function (x, ...)
>    sapply(x, mean, ...)
>    <environment: namespace:base>
>
> And indeed, it uses sapply() to apply the function mean to each
> column. You could have tried:
>
>    mean.default(temp[2,2:3])
>
> but this returns and error because it needs a numeric vector and does
> not automatically convert your data.frame into it. So you need:
>
>    mean.default(as.numeric(temp[2,2:3]))

Or:

mean( data.matrix(temp)[ 2 , 2:3] )


>
> or more simply
>
>    mean(as.numeric(temp[2,2:3]))
>
> I hope that helped. Sincerely,
>
> JiHO
> ---
> http://maururu.net
>
> ______________________________________________
> [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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[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.table or read.csv without row index?

vincent.deluard
In reply to this post by JiHO-2

YES IT WORKED!!!

Many thanks JiHO

 

--------------------------------------------

Vincent Deluard

[hidden email]

Global Equity Strategist, CFA Charter Award Pending

TrimTabs Investment Research

40 Wall Street, 28th Floor

New York, NY 10005

Phone: (+1) 646-512-5616

 

From: JiHO-2 [via R] [mailto:[hidden email]]
Sent: Wednesday, May 05, 2010 3:51 PM
To: vincent.deluard
Subject: Re: read.table or read.csv without row index?

 

> I tried as.matrix but it did not help.

as.matrix() won't work because a matrix requires everything in it to
be of the same type (number, character, logical etc.). You do not have
only numbers in your data.frame, so it will convert everything to
character strings. If you try as.matrix(temp[,-1]) it should work
(assuming you only have characters in the first column, otherwise
remove all non-numeric columns).

But what you really want is to circumvent the fact that, on a
data.frame, mean works column-wise. In fact, when you call mean() on a
data.frame() it calls mean.data.frame(), which code you can see by
typing its name at the prompt:

    > mean.data.frame
    function (x, ...)
    sapply(x, mean, ...)
    <environment: namespace:base>

And indeed, it uses sapply() to apply the function mean to each
column. You could have tried:

    mean.default(temp[2,2:3])

but this returns and error because it needs a numeric vector and does
not automatically convert your data.frame into it. So you need:

    mean.default(as.numeric(temp[2,2:3]))

or more simply

    mean(as.numeric(temp[2,2:3]))

I hope that helped. Sincerely,

JiHO
---
http://maururu.net

______________________________________________
[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.


View message @ http://r.789695.n4.nabble.com/read-table-or-read-csv-without-row-index-tp883594p2131575.html
To unsubscribe from Re: read.table or read.csv without row index?, click here.