Filtering data in a dataframe based on criteria

后端 未结 2 1375
说谎
说谎 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 03:14

    Given a dataframe "dfrm" with the names of the cities in the 'city' column, the population in the "population" column and the average summer temperature in the "meanSummerT" column your request for the subset meeting those joint requirements would be met with any of these:

    subset( dfrm, population < 1e6 & meanSummerT > 70)
    dfrm[ which(dfrm$population < 1e6 & dfrm$meanSummerT > 70) , ]
    dfrm[ which( dfrm[[ 'population' ]] < 1e6 & dfrm[[ 'meanSummerT' ]] > 70) , ]
    

    If you wanted just the names of the cities meeting those joint criteria then these would work:

    subset( dfrm, population < 1e6 & meanSummerT > 70 , city)
    dfrm[ which(dfrm$population < 1e6 & dfrm$meanSummerT > 70) , "city" ]
    dfrm[ which(dfrm[['population']] < 1e6 & dfrm[['meanSummerT']] > 70) , "city" ]
    

    Note that the column names are not quoted in the subset or following the "$" operator but they are quoted inside "[[". And note that using which can be dangerous if no lines of data match because instead of getting no lines you will get the entire dataframe.

提交回复
热议问题