[R] inverse table

Ulrik Stervbo ulrik.stervbo at gmail.com
Wed Jun 15 18:46:54 CEST 2016


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]]



More information about the R-help mailing list