[R] Table to Data Frame

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu Jun 10 12:50:16 CEST 2004


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




More information about the R-help mailing list