Filtering data in a dataframe based on criteria

后端 未结 2 1410
说谎
说谎 2021-02-09 02:46

I am new to R and can\'t get to grips with this concept. Suppose I have a table loaded called \"places\" with 3 say columns - city, population and average summer temperature

2条回答
  •  星月不相逢
    2021-02-09 02:51

    You are looking for subset

    if your data is called mydata

    newdata <- subset(mydata, city < 1e6)
    

    Or you could use [, which is programatically safer

    newdata <- mydata[mydata$city < 1e6]
    

    For more than one condition use & or | where approriate

    You could also use the sqldf package to use sql

    library(sqldf)
    
    newdata <-  sqldf('select * from mydata where city > 1e6')
    

    Or you could use data.table which makes the syntax easier for [ (as well as being memory efficient)

    library(data.table)
    
    mydatatable <- data.table(mydata)
    newdata <- mydatatable[city > 1e6]
    

提交回复
热议问题