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]]
> On Jun 15, 2016, at 11:10 AM, Patrizio Frederic <frederic.patrizio at gmail.com> wrote: > > 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, > > PFHi, There is a function called expand.dft(), which I posted some years ago, which is a modification of a prior version, posted a few years before that. The updated version is here: https://stat.ethz.ch/pipermail/r-help/2009-January/378521.html If memory serves, that code has made its way into one or more packages on CRAN but I don't recall which at the moment. Regards, Marc Schwartz
Hi Patrizio, maybe there is a more efficient way, but you can loop over rows and columns like this ab.recon <- data.frame() ddd.rownames <- rownames(ddd) ddd.colnames <- colnames(ddd) for(cur.row in ddd.rownames){ for(cur.col in ddd.colnames){ times.found <- ddd[cur.row, cur.col] tmp.df <- data.frame(a = rep(cur.row, times.found), b = rep(cur.col, times.found)) ab.recon <- rbind(ab.recon, tmp.df) } } Hope this helps Ulrik On Wed, 15 Jun 2016 at 18:12 Patrizio Frederic <frederic.patrizio at gmail.com> wrote:> 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. >[[alternative HTML version deleted]]
Em Qua 15 jun. 2016, ?s 13:10, Patrizio Frederic escreveu:> 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 believe packages reshape and reshape2 could help, although I don't use them. 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) df <- expand.grid(dimnames(ddd), stringsAsFactors = FALSE) df$freq <- as.vector(ddd) ab2 <- as.matrix(df[rep(seq.int(nrow(df)), df$freq), 1:2]) all.equal(ab2[order(ab2[, c("a", "b")])], ab[order(ab[, c("a", "b")])]) Hope that helps, Leonardo Ferreira Fontenelle
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.
Thank you all, David's solution is the one that matches my taste the most. Patrizio On Wed, Jun 15, 2016 at 8:04 PM, David L Carlson <dcarlson at tamu.edu> wrote:> 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. >-- +--------------------------------------------------------------- | Patrizio Frederic, | http://morgana.unimore.it/frederic_patrizio/ +--------------------------------------------------------------- [[alternative HTML version deleted]]
That function, expand.dft is in the vcdExtra package On 6/15/2016 12:42 PM, Marc Schwartz wrote:> > > There is a function called expand.dft(), which I posted some years ago, which is a modification of a prior version, posted a few years before that. > > The updated version is here: > > https://stat.ethz.ch/pipermail/r-help/2009-January/378521.html > > If memory serves, that code has made its way into one or more packages on CRAN but I don't recall which at the moment. > > Regards, > > Marc Schwartz >-- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. & Chair, Quantitative Methods York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 4700 Keele Street Web: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA