[R] Table to Data Frame
Adaikalavan Ramasamy
ramasamy at cancer.org.uk
Thu Jun 10 13:11:47 CEST 2004
# Generate data
universe <- c(0, 0.17, 0.5, 1, 2.5, 3, 4)
prob <- c(94, 1, 1, 1, 1, 1, 1)/1000
tab <- list(NULL)
for(i in 1:6){ tab[[i]] <- table(sample(universe, 218, prob=prob,
rep=TRUE)) }
# Re-table
all <- unique(unlist(sapply(tab, function(x) names(x))))
retab <- t(sapply( tab, function(x) table( factor( rep( rownames(x), x)
, levels=all ))))
The key is to have the exactly the same names (stored in object all) in
all table and sapply will simplify it.
Uwe suggestion is probably best if you can regenerate the data.
Regards, Adai.
On Thu, 2004-06-10 at 11:50, Uwe Ligges wrote:
> Ko-Kang Kevin Wang wrote:
>
> > [Forwarding on behalf of a colleague]
> >
> > She's got a list with several tables:
> >
> >>tab <- list()
> >>for(i in 1:6) {
>
>
> Most easy solution:
> Make freq[i] a factor with all the levels that may appear. Then all
> tables have same dimension, e.g.:
>
> tab <- lapply(freq, function(x) table(
> factor(x, levels = c(0, 0.17, 0.3, 0.5, 1, 2.5, 3, 4))))
>
> Simplify yourself - I don't know much about "freq" ...
>
> Uwe
>
> > + tab[[i]] <- table(freq[i])
> > + }
> >
> >>tab
> >
> > [[1]]
> >
> > 0 0.17 0.3 0.5 1 2.5 3 4
> > 196 2 5 1 5 2 5 2
> >
> > [[2]]
> >
> > 0 0.17 0.3 1 2.5 3 4
> > 199 1 3 6 2 6 1
> >
> > [[3]]
> >
> > 0 0.5
> > 217 1
> >
> > [[4]]
> >
> > 0 2.5
> > 216 2
> >
> > [[5]]
> >
> > 0 0.17 0.3 0.5 1 2.5 3 4
> > 207 1 1 1 1 2 4 1
> >
> > [[6]]
> >
> > 0 0.17 3
> > 216 1 1
> >
> >
> > And would like to convert to a data frame, like:
> >
> > 0 0.17 0.3 0.5 1
> > 196 2 5 1 5
> > 199 1 3 0 6
> > 217 0 0 1 0
> > [snip]
> >
> >
> > Basically down the columns she'd like to have the counts. But because each
> > table in the list has got different number of columns, I've been unable to
> > convert them into a data frame for her.
> >
> > Any help would be greatly appreciated!
> >
> > Cheers,
> >
> > Kevin
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
More information about the R-help
mailing list