nanthinee
2011-Jul-08 04:24 UTC
[R] How to convert matrix with frequency & variable to vector?
Hi, I have a dataset: freq x 1 30 7 3 70 8 4 100 7 5 22 6 6 250 10 How do I convert into vector form (v<- 7,7,7,8,8,8,6,6,6,6) all "x" with the complete frequency? Transpose only does a single row but I want the full frequencies not just the variable transposed. Thanks, Nanthinee -- View this message in context: http://r.789695.n4.nabble.com/How-to-convert-matrix-with-frequency-variable-to-vector-tp3653196p3653196.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2011-Jul-08 04:33 UTC
[R] How to convert matrix with frequency & variable to vector?
On Jul 8, 2011, at 12:24 AM, nanthinee wrote:> Hi, > > I have a dataset: > > freq x > 1 30 7 > 3 70 8 > 4 100 7 > 5 22 6 > 6 250 10perhaps (untested): c( apply(ds,1,function(x) rep(x[2], x[1]) ) ) also perhaps (since I think rep is vectorised): rep(ds$x, ds$freq)> > How do I convert into vector form (v<- 7,7,7,8,8,8,6,6,6,6) all "x" > with the > complete frequency? Transpose only does a single row but I want the > full > frequencies not just the variable transposed. > > Thanks, > Nanthinee > > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-convert-matrix-with-frequency-variable-to-vector-tp3653196p3653196.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Joshua Wiley
2011-Jul-08 04:34 UTC
[R] How to convert matrix with frequency & variable to vector?
Hi Nanthinee, Use ?rep it will rep()licate each x, freq times. dat <- data.frame(freq = c(30, 70, 100, 22, 250), x = c(7, 8, 7, 6, 10)) total <- rep(dat$x, dat$freq)> length(total)[1] 472 Cheers, Josh On Thu, Jul 7, 2011 at 9:24 PM, nanthinee <nanthineej at gmail.com> wrote:> Hi, > > I have a dataset: > > ?freq ? x > 1 ?30 ? ?7 > 3 ?70 ? ?8 > 4 ?100 ?7 > 5 ?22 ? ?6 > 6 ?250 ?10 > > How do I convert into vector form (v<- 7,7,7,8,8,8,6,6,6,6) all "x" with the > complete frequency? Transpose only does a single row but I want the full > frequencies not just the variable transposed. > > Thanks, > Nanthinee > > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-convert-matrix-with-frequency-variable-to-vector-tp3653196p3653196.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles https://joshuawiley.com/