如何根据外部列表过滤表的行? [英] How to filter a table's row based on an external list?

查看:153
本文介绍了如何根据外部列表过滤表的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(1)我有一个大的R表读取超过10000行和10列。
$ b

(2)表的第3列包含医院的名称。 (3)我有一个医院名称列表,例如: (4)你能不能介绍一下步骤3中列出的所有行?



以下是我输入文件的一个简短示例;

 患者治疗医院回应
1 A YYY好
2 B YYY死$ b $ 3 A ZZZ好$ b $ 4 A万维网好
5 C UUU死

我有一份有兴趣进一步研究的医院名单,即YYY和UUU。如何用R生成如下输出表?

 患者治疗医院回应
1 A YYY好
2 B YYY Dead
5 C UUU Dead


解决方案

在%运算符中使用%。
$ b $ $ $ $ $ $ $ $ $ $#$样本数据
dat< - data.frame(患者= 1:5,治疗=字母[1:5],
hospital = c(yyy,yyy,zzz,www uuu),response = rnorm(5))

#我们希望对
goodHosp < - c(yyy,uuu)$做进一步的分析你可以直接在你的data.frame对象中索引:






  dat [dat $ hospital%in%goodHosp,] 

或使用subset命令:

 子集(dat,hospital%in%goodHosp)
code>


(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 duplicated or even more.

(3) I have a list of hospitals' name, e.g. 10 of them are needed to be study further.

(4) Could you mind to teach me how to extract all the rows in step1 with the names listed in step 3?

Here is a shorter example of my input file;

Patients Treatment Hospital Response 
1        A         YYY      Good 
2        B         YYY      Dead 
3        A         ZZZ      Good 
4        A         WWW      Good 
5        C         UUU      Dead

I have a list of hospital that I am interested to study further, i.e YYY and UUU. How to generate a output table as follows with R?

Patients Treatment Hospital Response 
1        A         YYY      Good 
2        B         YYY      Dead 
5        C         UUU      Dead

解决方案

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)

这篇关于如何根据外部列表过滤表的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆