s-news
[Top] [All Lists]

Subsetting data from a data frame

To: s-news@lists.biostat.wustl.edu
Subject: Subsetting data from a data frame
From: Joseph LeBouton <lebouton@msu.edu>
Date: Wed, 23 Jun 2004 11:02:48 -0400
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)
#I have a dataset of vegetation plots, structured as in the simplified
#       example below. I want to identify plots where certain species
#       occur, and extract all of the data (i.e., data refering to both
#       the species of interest AND all other species that occur) from
#       just those plots for further analysis.  I would like to avoid
#       having to boot the dataset back and forth to Access database if
#       at all possible; however, my subsetting abilities are sub-par in
#       Splus.

# I want to avoid turning each species into a separate column, because I
#       have 12 or so columns describing each occurrence of each species
#       within each plot.

# Any suggestions would be much appreciated!


#Example dataset:

plotid <- rep(1:10, each=6)
p1 <- c("A","A","B","B","C","B")
p2 <- c("B","B","B","C","C","C")

spp <- rep(c(p1,p2,p2,p2,p1),2)

ht <- rep(1:6, times=10)
whole.set <- data.frame(plotid, spp, ht)

#Subset desired is all data from each plot that includes spp="A":

#e.g.,:

plots.w.A <- unique(select.rows(whole.set$plotid, whole.set$spp=="A"))
plots.w.A

#SO with the list of plots to consider, "plots.w.A",
# how do I extract all data for plotid==plots.w.A from object
#       "whole.set"?

#I tried select.rows:

data.w.A1<- select.rows(whole.set, whole.set$plotid == plots.w.A)
data.w.A1

#and subsetting:

data.w.A2 <- (whole.set[whole.set$plotid == plots.w.A,])
data.w.A2

#both with unsatisfactory results.  What I need is all the data (in this
#case, 6 lines) from plots 1,5,6, and 10.  Any suggestions as to what
#I'm doing wrong?

#Thanks in advance,

#jlb


--
************************************
Joseph P. LeBouton
Forest Ecology PhD Candidate
Department of Forestry
Michigan State University
East Lansing, Michigan 48824

Office phone: 517-355-7744
email: lebouton@msu.edu


<Prev in Thread] Current Thread [Next in Thread>