Do not post to the list in html and always copy your message to the list so
others can follow the discussion.
That should not matter if the possible values in a row are only 0, 1, 2 as you
indicated. No matter how large the matrix is, the table should only have three
columns. If more values are possible you need to modify the colnames() statement
accordingly.
David
From: Lida Zeighami [mailto:lid.zigh at gmail.com]
Sent: Friday, June 26, 2015 2:36 PM
To: David L Carlson
Subject: Re: [R] question
David,
Thank you so much for your help.
just when I inter this line :??? > colnames(tbl) <-
paste0("fre", 0:2)
it?gets "Error in `colnames<-`(`*tmp*`, value = c("fre0",
"fre1", "fre2")) :
? length of 'dimnames' [2] not equal to array extent"
I think it because of my matrix is 5718*25761? the name of rows is in this
format: A00002......A00045.....
and my colnames are in this format:?? 1:866453????? ........?? 21:878632
so would you please let me know what is the reason of my error and how can I
correct it?
Thanks again,
Lida
On Fri, Jun 26, 2015 at 2:19 PM, David L Carlson <dcarlson at tamu.edu>
wrote:
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.