[R] Extract values from data frame in R
Using R, I would like to find out which Samples (S1, S2, S3, S4, S5) fulfill the following criteria:contain minimally one value (x, y or z) bigger than 4. Any ideas? Thanks, Alex. data Sample xy z 1S1 -0.35.32.5 2S20.40.2 -1.2 3S31.2 -0.63.2 4S44.30.75.7 5S52.44.32.3 [[alternative HTML version deleted]] __ 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.
Re: [R] Extract values from data frame in R
On 08/09/2010 01:16 AM, Alexander Eggel wrote: Using R, I would like to find out which Samples (S1, S2, S3, S4, S5) fulfill the following criteria:contain minimally one value (x, y or z) bigger than 4. Any ideas? Thanks, Alex. data Sample xy z 1S1 -0.35.32.5 2S20.40.2 -1.2 3S31.2 -0.63.2 4S44.30.75.7 5S52.44.32.3 Untested: Sample[apply(Sample[-1], 1, function(x) any(x) 4)), Sample] __ 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.
Re: [R] Extract values from data frame in R
On Aug 9, 2010, at 2:16 AM, Alexander Eggel wrote: Using R, I would like to find out which Samples (S1, S2, S3, S4, S5) fulfill the following criteria:contain minimally one value (x, y or z) bigger than 4. Any ideas? Thanks, Alex. data Sample xy z 1S1 -0.35.32.5 2S20.40.2 -1.2 3S31.2 -0.63.2 4S44.30.75.7 5S52.44.32.3 data$Sample[apply(data[, -1], 1, function(xyz) any(xyz 4))] [1] S1 S4 S5 David Winsemius, MD West Hartford, CT __ 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.
Re: [R] Extract values from data frame in R
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Erik Iverson Sent: Sunday, August 08, 2010 11:27 PM To: Alexander Eggel Cc: r-help@r-project.org Subject: Re: [R] Extract values from data frame in R On 08/09/2010 01:16 AM, Alexander Eggel wrote: Using R, I would like to find out which Samples (S1, S2, S3, S4, S5) fulfill the following criteria:contain minimally one value (x, y or z) bigger than 4. Any ideas? Thanks, Alex. data Sample xy z 1S1 -0.35.32.5 2S20.40.2 -1.2 3S31.2 -0.63.2 4S44.30.75.7 5S52.44.32.3 Untested: Sample[apply(Sample[-1], 1, function(x) any(x) 4)), Sample] The any(x)4 should be any(x4), as in: f1 - function(data) data[apply(data[-1], 1, function(x) any(x 4)), Sample] Note that operating a column at a time on a data is often faster than operating a row at a time. E.g., f2 - function(data) with(data, Sample[x4 | y4 | z4]) makeData - function (nrow, seed){ if (!missing(seed)) set.seed(seed) data.frame(Sample = sample(paste(S, 1:5, sep = ), replace = TRUE, size = nrow), x = rgamma(nrow, 4), y = rgamma(nrow, 5), z = rgamma(nrow, 3)) } z - makeData(1, seed=73) system.time(v1 - f1(z)) user system elapsed 0.270.000.25 system.time(v2 - f2(z)) user system elapsed 0.000.010.01 identical(v1, v2) [1] TRUE length(v1) [1] 8390 (I prefer that non-apply approach because apply often causes trouble when used with data.frames -- it is only safe when all columns are numeric.) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com __ 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. __ 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.