[R] How to subset() from data frame using specific rows
Sarah Goslee
sarah.goslee at gmail.com
Tue Oct 4 20:46:08 CEST 2011
Hi Rich,
You can use something like this:
> testdata <- c("A1", "A2", "A3", "B1", "B2", "B3")
> grep("^A", testdata)
[1] 1 2 3
> grepl("^A", testdata)
[1] TRUE TRUE TRUE FALSE FALSE FALSE
Sarah
On Tue, Oct 4, 2011 at 2:39 PM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> I have a data frame called chemdata with this structure:
>
>> str(chemdata)
>
> 'data.frame': 14886 obs. of 4 variables:
> $ site : Factor w/ 148 levels "BC-0.5","BC-1",..: 104 145 126 115 114
> 128 124 2 3 3 ...
> $ sampdate: Date, format: "1996-12-27" "1996-08-22" ...
> $ param : Factor w/ 8 levels "As","Ca","Cl",..: 1 1 1 1 1 1 1 1 1 1 ...
> $ quant : num 0.06 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 ...
>
> I've looked in the R Cookbook and Dalgaard's intro book without finding a
> way to use wildcards (e.g., like "BC-*") or explicitly witing each site ID
> when subdsetting a data frame..
>
> I need to create subsets (as data frames) based on sites, but including
> all sites on each stream. For example, using the initial site factor shown
> above, I want a subset containing all data for sites "BC-0.5", "BC-1".
> "BC-2", "BC-3", "BC-4", "BC-5", and "BC-6".
>
> Pointers appreciated,
>
> Rich
>
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list