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

后端 未结 2 1446
夕颜
夕颜 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: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)
    

提交回复
热议问题