Hello, Say I have 2 columns, bmi and gender, the first being all the values and the second being male or female. How would I subset this into males only and females only? I have searched these fora and read endlessly about select[] and split() functions but to no avail. Also the table is not ordered. bmi gender -> bmi gender + bmi gender 1 24.78 male 1 24.78 male 3 23.18 female 2 26.42 male 2 26.42 male 4 22.36 female 3 23.18 female ... ... 4 22.36 female ... Thank you in advance -- View this message in context: http://r.789695.n4.nabble.com/Creating-subsets-of-a-matrix-tp3232838p3232838.html Sent from the R help mailing list archive at Nabble.com.
Hi, Try looking at ?subset I think something like this should work: dat.male <- subset(dat, gender == "male") dat.female <- subset(dat, gender == "female") though it may require tweaking depending how your data is stored. If you want more specific help, suppose your matrix is called "dat" (but substitute the actual name of it in), send us (or me) the output from: dput(dat) or if that is incredibly long, just str(dat) Here is a little example with a builtin dataset using subset(): print(mtcars) # show the data ## save and show all rows where column 'vs' is equal to 1 (dat.1 <- subset(mtcars, vs == 1)) ## save and show all rows where column 'vs' is equal to 0 (dat.0 <- subset(mtcars, vs == 0)) Hope this helps, Josh On Sun, Jan 23, 2011 at 10:36 AM, poolmunch <poolmunch at gmail.com> wrote:> > Hello, > > Say I have 2 columns, bmi and gender, the first being all the values and the > second being male or female. How would I subset this into males only and > females only? I have searched these fora and read endlessly about select[] > and split() functions but to no avail. Also the table is not ordered. > > ? ? bmi gender ? ? ? ?-> ? ? ?bmi gender ? ? ? ? + ? ?bmi ?gender > 1 ?24.78 ? male ? ? ? ? ? ? ?1 24.78 ? male ? ? ? ? ? 3 23.18 ?female > 2 ?26.42 ? male ? ? ? ? ? ? ?2 26.42 ? male ? ? ? ? ? 4 22.36 ?female > 3 ?23.18 female ? ? ? ? ? ? ?... ? ? ? ? ? ? ? ? ? ? ? ? ?... > 4 ?22.36 female > ... > > Thank you in advance > -- > View this message in context: http://r.789695.n4.nabble.com/Creating-subsets-of-a-matrix-tp3232838p3232838.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 http://www.joshuawiley.com/
Try 'split'> xbmi gender 1 24.78 male 2 26.42 male 3 23.18 female 4 22.36 female> # create a list with the split genders > split(x, x$gender)$female bmi gender 3 23.18 female 4 22.36 female $male bmi gender 1 24.78 male 2 26.42 male> >On Sun, Jan 23, 2011 at 1:36 PM, poolmunch <poolmunch at gmail.com> wrote:> > Hello, > > Say I have 2 columns, bmi and gender, the first being all the values and the > second being male or female. How would I subset this into males only and > females only? I have searched these fora and read endlessly about select[] > and split() functions but to no avail. Also the table is not ordered. > > ? ? bmi gender ? ? ? ?-> ? ? ?bmi gender ? ? ? ? + ? ?bmi ?gender > 1 ?24.78 ? male ? ? ? ? ? ? ?1 24.78 ? male ? ? ? ? ? 3 23.18 ?female > 2 ?26.42 ? male ? ? ? ? ? ? ?2 26.42 ? male ? ? ? ? ? 4 22.36 ?female > 3 ?23.18 female ? ? ? ? ? ? ?... ? ? ? ? ? ? ? ? ? ? ? ? ?... > 4 ?22.36 female > ... > > Thank you in advance > -- > View this message in context: http://r.789695.n4.nabble.com/Creating-subsets-of-a-matrix-tp3232838p3232838.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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?