Grokbase Groups R r-help January 2012
FAQ
How do I subset data to only keep those rows of a dataframe where a
variable's value matches one item of a vector. For example, how do I keep
all of the rows (and all variables) where mydata$id equals one of the values
in keepid? See below?

mydata <- NULL
mydata$id <- 1:30
mydata$value <- seq(from=1,to0, length.out0)
keepid <- c(6,10,12,13,19,25,26,27,28,29)

In other words, I want the output file to have two columns, 1) id and 2)
value, with only those rows where id equals one of the values in the keepid
vector. Thanks for your help!

--
View this message in context: http://r.789695.n4.nabble.com/Keep-rows-where-a-variable-matches-one-item-of-a-vector-tp4290297p4290297.html
Sent from the R help mailing list archive at Nabble.com.

Search Discussions

  • Sarah Goslee at Jan 12, 2012 at 8:57 pm
    Hi,
    On Thu, Jan 12, 2012 at 3:10 PM, dadrivr wrote:
    How do I subset data to only keep those rows of a dataframe where a
    variable's value matches one item of a vector. ?For example, how do I keep
    all of the rows (and all variables) where mydata$id equals one of the values
    in keepid? ?See below?

    mydata <- NULL
    mydata$id <- 1:30
    mydata$value <- seq(from=1,to0, length.out0)
    keepid <- c(6,10,12,13,19,25,26,27,28,29)
    You won't get two columns unless you create a data frame rather than a
    regular list.

    mydata <- data.frame(id = 1:30, value = seq(from=1,to0, length.out0))
    In other words, I want the output file to have two columns, 1) id and 2)
    value, with only those rows where id equals one of the values in the keepid
    vector. ?Thanks for your help!
    Here are two ways; more exist.

    mydata[mydata$id %in% keepid, ]
    subset(mydata, mydata$id %in% keepid)

    Thanks for including a small reproducible example.

    Sarah

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-help @
categoriesr
postedJan 12, '12 at 8:10p
activeJan 12, '12 at 8:57p
posts2
users2
websiter-project.org
irc#r

2 users in discussion

Dadrivr: 1 post Sarah Goslee: 1 post

People

Translate

site design / logo © 2023 Grokbase