try:
x.1 <- whole.set$plotid[whole.set$spp=="A"]
x.1
[1] 1 1 5 5 6 6 10 10
whole.set[whole.set$plotid %in% x.1,] # chose just those rows that
match on the plotid
__________________________________________________________
James Holtman "What is the problem you are trying to solve?"
Executive Technical Consultant -- Office of Technology, Convergys
james.holtman@convergys.com
+1 (513) 723-2929
Joseph LeBouton
<lebouton@msu.edu> To: s-news@lists.biostat.wustl.edu
Sent by: cc:
s-news-owner@lists.biosta Subject: [S] Subsetting data from a data frame
t.wustl.edu
06/23/2004 11:02
#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
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news