Hi members, I'd like to thank you guys ahead of time for the help. I'm kind of stuck. I have a data frame with ID and position numbers: 1> head(failed.3) id position 1 10000997 2 4 1000RW_M 2 15 1006RW_G 2 24 1012RW_M 3 28 10160917 2 30 1016RW_M 13 I'd like to use this to subset out a large dataset and keep only the observation number corresponding to the position number. So for example, ID 10000997 has 10 observations. I want to keep the 2nd one only. Thanks, -linh
jdnewmil@gmail.com
2011-Mar-05 19:02 UTC
[R] subsetting data by specified observation number
What is wrong with subset( failed.3, position == 2 ) ? -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. Linh Tran <Tranlm@berkeley.edu> wrote: Hi members, I'd like to thank you guys ahead of time for the help. I'm kind of stuck. I have a data frame with ID and position numbers: 1> head(failed.3) id position 1 10000997 2 4 1000RW_M 2 15 1006RW_G 2 24 1012RW_M 3 28 10160917 2 30 1016RW_M 13 I'd like to use this to subset out a large dataset and keep only the observation number corresponding to the position number. So for example, ID 10000997 has 10 observations. I want to keep the 2nd one only. Thanks, -linh_____________________________________________ 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. [[alternative HTML version deleted]]
Will failed.3 have each id exactly once? Or could it have multiple lines for a given id? -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Linh Tran > Sent: Saturday, March 05, 2011 11:56 AM > To: r-help at r-project.org > Subject: [R] subsetting data by specified observation number > > Hi members, > > I'd like to thank you guys ahead of time for the help. I'm kind of > stuck. > > I have a data frame with ID and position numbers: > 1> head(failed.3) > id position > 1 10000997 2 > 4 1000RW_M 2 > 15 1006RW_G 2 > 24 1012RW_M 3 > 28 10160917 2 > 30 1016RW_M 13 > > I'd like to use this to subset out a large dataset and keep only the > observation number corresponding to the position number. So for > example, > ID 10000997 has 10 observations. I want to keep the 2nd one only. > > > Thanks, > > -linh > > ______________________________________________ > 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.
Here is one way:> tmp1 <- data.frame(Species=c('setosa','virginica','versicolor'),+ row=c(7,20,18) )> > tmp.iris <- iris > tmp.iris$row <- ave(iris$Sepal.Length, iris$Species, FUN=seq_along) > > out.iris <- merge(tmp.iris, tmp1, by=c('Species','row')) > > > out.irisSpecies row Sepal.Length Sepal.Width Petal.Length Petal.Width 1 setosa 7 4.6 3.4 1.4 0.3 2 versicolor 18 5.8 2.7 4.1 1.0 3 virginica 20 6.0 2.2 5.0 1.5>Another way would be to use the split function on your big data set, then use sapply to iterate over the list resulting and return just the rows from failed.3 in each group. Need to think a bit more about how that would look. You could also just loop through the rows of failed.3 and grab the corresponding pieces in the full dataset. There are probably a few other ways as well. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Linh Tran > Sent: Saturday, March 05, 2011 11:56 AM > To: r-help at r-project.org > Subject: [R] subsetting data by specified observation number > > Hi members, > > I'd like to thank you guys ahead of time for the help. I'm kind of > stuck. > > I have a data frame with ID and position numbers: > 1> head(failed.3) > id position > 1 10000997 2 > 4 1000RW_M 2 > 15 1006RW_G 2 > 24 1012RW_M 3 > 28 10160917 2 > 30 1016RW_M 13 > > I'd like to use this to subset out a large dataset and keep only the > observation number corresponding to the position number. So for > example, > ID 10000997 has 10 observations. I want to keep the 2nd one only. > > > Thanks, > > -linh > > ______________________________________________ > 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.