Count or summary data

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

Count or summary data

Xiyan Lon
Dear all,
I want to summary and count my data something like
> te.Ce
      [,1] [,2]
 [1,]   -1 0.05
 [2,]    1 0.05
 [3,]    1 0.00
 [4,]    0 0.05
 [5,]   -1 0.00
 [6,]    0 0.10
 [7,]    1 0.10
 [8,]   -1 0.00
 [9,]   -1 0.10
[10,]    0 0.05
[11,]    0 0.10
[12,]   -1 0.10
[13,]    1 0.00
[14,]   -1 0.05
[15,]    1 0.00

How could I count (summary) all my data which I need the result like

for 0.05
-1  0  1
 2  2  1

for 0.00
-1  0  1
 2  0  3

for 0.10
-1  0  1
 2  2  1

I have tried with summary but I did not find what I need.
Maybe someone could help me.
Happy new year.
Xiyan Lon

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

Re: Count or summary data

bogdan romocea
Here's one approach,
v1 <- sample(c(-1,0,1),30,replace=TRUE)
v2 <- sample(c(0.05,0,0.1),30,replace=TRUE)
lst <- split(v1,v2)
counted <- lapply(lst,table)
mat <- do.call("rbind",counted)
print(counted)
print(mat)


> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Xiyan Lon
> Sent: Friday, December 30, 2005 9:48 AM
> To: [hidden email]
> Subject: [R] Count or summary data
>
>
> Dear all,
> I want to summary and count my data something like
> > te.Ce
>       [,1] [,2]
>  [1,]   -1 0.05
>  [2,]    1 0.05
>  [3,]    1 0.00
>  [4,]    0 0.05
>  [5,]   -1 0.00
>  [6,]    0 0.10
>  [7,]    1 0.10
>  [8,]   -1 0.00
>  [9,]   -1 0.10
> [10,]    0 0.05
> [11,]    0 0.10
> [12,]   -1 0.10
> [13,]    1 0.00
> [14,]   -1 0.05
> [15,]    1 0.00
>
> How could I count (summary) all my data which I need the result like
>
> for 0.05
> -1  0  1
>  2  2  1
>
> for 0.00
> -1  0  1
>  2  0  3
>
> for 0.10
> -1  0  1
>  2  2  1
>
> I have tried with summary but I did not find what I need.
> Maybe someone could help me.
> Happy new year.
> Xiyan Lon
>
> ______________________________________________
> [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
>

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

Re: Count or summary data

Gabor Grothendieck
In reply to this post by Xiyan Lon
Assuming te.Ce is a data frame:

table(te.Ce)


On 12/30/05, Xiyan Lon <[hidden email]> wrote:

>
> Dear all,
> I want to summary and count my data something like
> > te.Ce
>      [,1] [,2]
> [1,]   -1 0.05
> [2,]    1 0.05
> [3,]    1 0.00
> [4,]    0 0.05
> [5,]   -1 0.00
> [6,]    0 0.10
> [7,]    1 0.10
> [8,]   -1 0.00
> [9,]   -1 0.10
> [10,]    0 0.05
> [11,]    0 0.10
> [12,]   -1 0.10
> [13,]    1 0.00
> [14,]   -1 0.05
> [15,]    1 0.00
>
> How could I count (summary) all my data which I need the result like
>
> for 0.05
> -1  0  1
> 2  2  1
>
> for 0.00
> -1  0  1
> 2  0  3
>
> for 0.10
> -1  0  1
> 2  2  1
>
> I have tried with summary but I did not find what I need.
> Maybe someone could help me.
> Happy new year.
> Xiyan Lon
>

        [[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
Reply | Threaded
Open this post in threaded view
|

Re: Count or summary data

jholtman
In reply to this post by Xiyan Lon
Here is one way and how to access the data:


> x
      [,1] [,2]
 [1,]   -1 0.05
 [2,]    1 0.05
 [3,]    1 0.00
 [4,]    0 0.05
 [5,]   -1 0.00
 [6,]    0 0.10
 [7,]    1 0.10
 [8,]   -1 0.00
 [9,]   -1 0.10
[10,]    0 0.05
[11,]    0 0.10
[12,]   -1 0.10
[13,]    1 0.00
[14,]   -1 0.05
[15,]    1 0.00
> tapply(x[,1], x[,2], table)
$"0"

-1  1
 2  3

$"0.05"

-1  0  1
 2  2  1

$"0.1"

-1  0  1
 2  2  1

> y <- tapply(x[,1], x[,2], table)
> y[["0"]]["-1"]
-1
 2
> y[["0.1"]]["0"]
0
2
>



On 12/30/05, Xiyan Lon <[hidden email]> wrote:

>
> Dear all,
> I want to summary and count my data something like
> > te.Ce
>      [,1] [,2]
> [1,]   -1 0.05
> [2,]    1 0.05
> [3,]    1 0.00
> [4,]    0 0.05
> [5,]   -1 0.00
> [6,]    0 0.10
> [7,]    1 0.10
> [8,]   -1 0.00
> [9,]   -1 0.10
> [10,]    0 0.05
> [11,]    0 0.10
> [12,]   -1 0.10
> [13,]    1 0.00
> [14,]   -1 0.05
> [15,]    1 0.00
>
> How could I count (summary) all my data which I need the result like
>
> for 0.05
> -1  0  1
> 2  2  1
>
> for 0.00
> -1  0  1
> 2  0  3
>
> for 0.10
> -1  0  1
> 2  2  1
>
> I have tried with summary but I did not find what I need.
> Maybe someone could help me.
> Happy new year.
> Xiyan Lon
>
> ______________________________________________
> [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
>



--
Jim Holtman
Cincinnati, OH
+1 513 247 0281

What the problem you are trying to solve?

        [[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
Reply | Threaded
Open this post in threaded view
|

Re: Count or summary data

Marc Schwartz (via MN)
In reply to this post by Xiyan Lon
On Fri, 2005-12-30 at 15:47 +0100, Xiyan Lon wrote:

> Dear all,
> I want to summary and count my data something like
> > te.Ce
>       [,1] [,2]
>  [1,]   -1 0.05
>  [2,]    1 0.05
>  [3,]    1 0.00
>  [4,]    0 0.05
>  [5,]   -1 0.00
>  [6,]    0 0.10
>  [7,]    1 0.10
>  [8,]   -1 0.00
>  [9,]   -1 0.10
> [10,]    0 0.05
> [11,]    0 0.10
> [12,]   -1 0.10
> [13,]    1 0.00
> [14,]   -1 0.05
> [15,]    1 0.00
>
> How could I count (summary) all my data which I need the result like
>
> for 0.05
> -1  0  1
>  2  2  1
>
> for 0.00
> -1  0  1
>  2  0  3
>
> for 0.10
> -1  0  1
>  2  2  1
>
> I have tried with summary but I did not find what I need.
> Maybe someone could help me.
> Happy new year.
> Xiyan Lon

There are several options, depending upon the output format you require.

The easiest is probably to use table() to generate a crosstabs of the
two columns:

> table(te.Ce[, 2], te.Ce[, 1])

       -1 0 1
  0     2 0 3
  0.05  2 2 1
  0.1   2 2 1


Then, there are by() and tapply(), each of which subsets the matrix by
the value in the second column, resulting in the following:

> by(te.Ce[, 1], te.Ce[, 2], table)
INDICES: 0

-1  1
 2  3
------------------------------------------------------
INDICES: 0.05

-1  0  1
 2  2  1
------------------------------------------------------
INDICES: 0.1

-1  0  1
 2  2  1



> tapply(te.Ce[, 1], te.Ce[, 2], table)
$"0"

-1  1
 2  3

$"0.05"

-1  0  1
 2  2  1

$"0.1"

-1  0  1
 2  2  1


See ?table, ?by and ?tapply for more information.

HTH,

Marc Schwartz

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