Hi r-help! Consider the following data-frame: var1 var2 var3 1 3 1 4 2 2 2 3 3 2 2 3 4 4 4 NA 5 4 3 5 6 2 2 3 7 3 4 3 How can I get R to convert this into the following? Value 1 2 3 4 5 var1 0 3 2 2 0 var2 1 3 1 2 0 var3 0 0 4 1 1 TIA, -- Hans Ekbrand (http://sociologi.cjb.net) <hans at sociologi.cjb.net> Q. What is that strange attachment in this mail? A. My digital signature, see www.gnupg.org for info on how you could use it to ensure that this mail is from me and has not been altered on the way to you. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: Digital signature URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090217/e120b501/attachment-0002.bin>
on 02/17/2009 09:06 AM Hans Ekbrand wrote:> Hi r-help! > > Consider the following data-frame: > > var1 var2 var3 > 1 3 1 4 > 2 2 2 3 > 3 2 2 3 > 4 4 4 NA > 5 4 3 5 > 6 2 2 3 > 7 3 4 3 > > How can I get R to convert this into the following? > > Value 1 2 3 4 5 > var1 0 3 2 2 0 > var2 1 3 1 2 0 > var3 0 0 4 1 1> t(sapply(DF, function(x) table(factor(x, levels = 1:5))))1 2 3 4 5 var1 0 3 2 2 0 var2 1 3 1 2 0 var3 0 0 4 1 1 The key is to turn each column into a factor with explicitly defined common levels for tabulation. This enables the table result to have a consistent format across each column, allowing for a matrix to be created, rather than a list. HTH, Marc Schwartz