[R] inverse table

David L Carlson dcarlson at tamu.edu
Wed Jun 15 20:04:44 CEST 2016


After converting the table to a data frame, replicate each row by the number of observations:

> ddd.df <- as.data.frame(ddd)  # as.data.frame.table does the same thing
> ddd.new <- as.matrix(ddd.df[rep(seq_along(ddd.df[, 1]), ddd.df$Freq), 1:2])
> head(ddd.new)
    a   b  
1   "a" "A"
1.1 "a" "A"
2   "b" "A"
2.1 "b" "A"
3   "c" "A"
4   "d" "A"
> rownames(ddd.new) <- NULL # Optional - get rid of row names
> head(ddd.new)
     a   b  
[1,] "a" "A"
[2,] "a" "A"
[3,] "b" "A"
[4,] "b" "A"
[5,] "c" "A"
[6,] "d" "A"

-------------------------------------
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 Patrizio Frederic
Sent: Wednesday, June 15, 2016 11:10 AM
To: r-help at r-project.org
Subject: [R] inverse table

Dear R-users,
I've a problem that puzzle me

suppose I have a two way contigency  table

a <- sample(al <- letters[1:10],100,T)
b <- sample(bl <- LETTERS[1:5],100,T)
ab <- cbind(a,b)

ddd <- (xtabs(data = ab))
ddd <- as.matrix(ddd)

the question is: how do I reverse the code, thus how do I get raw data
(object ab) from ddd?

I've tried

as.data.frame.table(ddd)

which is not the answer I'm looking for.
Thanks in advance,

PF



-- 
+---------------------------------------------------------------
| Patrizio Frederic,
| http://morgana.unimore.it/frederic_patrizio/
+---------------------------------------------------------------

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



More information about the R-help mailing list