Given a dataframe m> mX Y V3 V4 1 1 A 0.5 1.2 2 1 B 0.2 1.4 3 2 A 0.1 0.9 How do I convert m to this with V4 as the cell values ? A B 1 1.2 1.4 2 0.9 NA
Henrique Dallazuanna
2008-Oct-30 12:07 UTC
[R] how to convert data from long to wide format ?
Try this: xtabs(V4 ~ X + Y, data = m) On Thu, Oct 30, 2008 at 9:29 AM, Daren Tan <daren76@hotmail.com> wrote:> > Given a dataframe m > > m > X Y V3 V4 > 1 1 A 0.5 1.2 > 2 1 B 0.2 1.4 > 3 2 A 0.1 0.9 > > How do I convert m to this with V4 as the cell values ? > > A B > 1 1.2 1.4 > 2 0.9 NA > > ______________________________________________ > R-help@r-project.org mailing list > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
You can look at the reshape package:> xX Y V3 V4 1 1 A 0.5 1.2 2 1 B 0.2 1.4 3 2 A 0.1 0.9> z <- melt(x[,-3]) # ignore V3Using X, Y as id variables> zX Y variable value 1 1 A V4 1.2 2 1 B V4 1.4 3 2 A V4 0.9> cast(z, X ~ Y, sum)X A B 1 1 1.2 1.4 2 2 0.9 NA>On Thu, Oct 30, 2008 at 7:29 AM, Daren Tan <daren76 at hotmail.com> wrote:> > Given a dataframe m >> m > X Y V3 V4 > 1 1 A 0.5 1.2 > 2 1 B 0.2 1.4 > 3 2 A 0.1 0.9 > > How do I convert m to this with V4 as the cell values ? > > A B > 1 1.2 1.4 > 2 0.9 NA > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Seemingly Similar Threads
- Speeding up casting a dataframe from long to wide format
- Reshape matrix from wide to long format
- Any simple way to subset a vector of strings that do contain a particular substring ?
- Identifying common prefixes from a vector of words, and delete those prefixes
- counting number of "G" in "TCGGGGGACAATCGGTAACCCGTCT"