R: Select values from data table in range

前端 未结 3 748
生来不讨喜
生来不讨喜 2020-12-04 14:24

I have a data table in R:

name    date
----    ----
John    1156649280
Adam    1255701960
...etc...

I want to get all of the rows that have

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 15:22

    Lots of options here, but one of the easiest to follow is subset. Consider:

    > set.seed(43)
    > df <- data.frame(name = sample(letters, 100, TRUE), date = sample(1:500, 100, TRUE))
    > 
    > subset(df, date > 5 & date < 15)
       name date
    11    k   10
    67    y   12
    86    e    8
    

    You can also insert logic directly into the index for your data.frame. The comma separates the rows from columns. We just have to remember that R indexes rows first, then columns. So here we are saying rows with date > 5 & < 15 and then all columns:

    df[df$date > 5 & df$date < 15 ,]
    

    I'd also recommend checking out the help pages for subset, ?subset and the logical operators ?"&"

提交回复
热议问题