[R] question

David L Carlson
Fri, 26 Jun 2015 19:19:41 -0000

Don't use html formatting in your emails and use dput() to provide data.
Assuming your matrix is called mat:

> mat <- structure(c(0L, 1L, 2L, 1L, 0L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
0L, 1L, 0L, 2L, 0L, 0L, 1L, 2L, 0L, 0L, 0L, 2L, 2L, 0L, 0L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), .Dim = c(5L, 7L), .Dimnames = list(c("A",
"B", "C", "D", "E"), c("1",
"2", "3", "4", "5", "6",
"7")))
> mat
  1 2 3 4 5 6 7
A 0 1 1 0 2 2 2
B 1 1 1 2 0 0 2
C 2 1 1 0 0 0 2
D 1 1 0 0 0 2 2
E 0 2 1 1 2 2 2
> tbl <- t(apply(mat, 1, table))
> colnames(tbl) <- paste0("fre", 0:2)
> tbl
  fre0 fre1 fre2
A    2    2    3
B    2    3    2
C    3    2    2
D    3    2    2
E    1    2    4
> matbl <- cbind(mat, tbl)
> matbl
  1 2 3 4 5 6 7 fre0 fre1 fre2
A 0 1 1 0 2 2 2    2    2    3
B 1 1 1 2 0 0 2    2    3    2
C 2 1 1 0 0 0 2    3    2    2
D 1 1 0 0 0 2 2    3    2    2
E 0 2 1 1 2 2 2    1    2    4

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Lida Zeighami
Sent: Friday, June 26, 2015 1:11 PM
To: r-help at r-project.org
Subject: [R] question

Hi there,

I have a matrix (n*m) which rows including 0,1,2
I want to know the frequency of each elements (0 , 1 , 2) separately for
each row!
for example :

     1    2    3   4    5    6     7

A   0     1   1    0     2    2    2
B   1     1   1    2    0    0     2
C   2    1   1    0     0    0    2
D   1     1   0    0    0    2     2
E   0     2   1    1     2    2    2

I want to this output:

     1    2    3   4    5    6     7         fr0
fr1               fr2

A   0     1   1    0     2    2    2           2                  2
     3
B   1     1   1    2    0    0     2           3
2             2
C   2    1   1    0     0    0     2          3                   2
     2
D   1     1   0    0    0    2     2           2                  3
        2
E   0     2   1    1     2    2    2           1
2            4

Thanks

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.