How to filter a table's row based on an external list?

后端 未结 2 1447
夕颜
夕颜 2020-12-15 17:22

(1) I have a large table read in R with more than a 10000 of rows and 10 columns.

(2) The 3rd column of the table contain the name of the hospitals. Some of them are

相关标签:
2条回答
  • 2020-12-15 17:52

    Using dplyr

    Setting up Data --- using @Chase's sample data.

    #Sample data
    df <- data.frame(patients = 1:5, treatment = letters[1:5],
      hospital = c("yyy", "yyy", "zzz", "www", "uuu"), response = rnorm(5))
    
    #List of hospitals we want to do further analysis on
    goodHosp <- c("yyy", "uuu")
    

    Now filter data using dplyr filter

    library(dplyr)
    df %>% filter(hospital %in% goodHosp)
    
    0 讨论(0)
  • 2020-12-15 17:57

    Use the %in% operator.

    #Sample data
    dat <- data.frame(patients = 1:5, treatment = letters[1:5],
      hospital = c("yyy", "yyy", "zzz", "www", "uuu"), response = rnorm(5))
    
    #List of hospitals we want to do further analysis on
    goodHosp <- c("yyy", "uuu")
    

    You can either index directly into your data.frame object:

    dat[dat$hospital %in% goodHosp ,]
    

    or use the subset command:

    subset(dat, hospital %in% goodHosp)
    
    0 讨论(0)
提交回复
热议问题