从多个数据帧R创建数据帧

时间:2014-01-06 19:38:52

标签: r dataframe

我的数据如下:

'data.frame':   798 obs. of  16 variables:
 $ READMIT.ENCOUNTER   : int  (intentionally blank)
 $ MRN                 : int  (intentionally blank)
 $ READMIT.SOURCE      : Factor w/ 5 levels "Emergency Unit",..: 5 1 1 1 5 1 1 1 1 1 ...
 $ READMIT.DATE        : Factor w/ 300 levels "1/1/2013","1/11/2013",..: 83 25 92 88 4 92 26 18 24 9 ...
 $ READMIT.DISC.DATE   : Factor w/ 284 levels "1/10/2013","1/11/2013",..: 10 8 22 22 14 26 3 25 1 112 ...
 $ READMIT.MONTH       : int  1 1 1 1 1 1 1 1 1 2 ...
 $ READMIT.YEAR        : int  2013 2013 2013 2013 2013 2013 2013 2013 2013 2013 ...
 $ LOS                 : int  11 10 4 7 11 8 4 4 3 18 ...
 $ INTERIM             : int  24 27 27 19 29 4 21 9 18 18 ...
 $ READMIT.INSURANCE   : Factor w/ 33 levels "A12","A13","B03",..: 1 2 1 23 9 4 19 2 2 2 ...
 $ READMIT.MDC         : Factor w/ 18 levels "CIRCULATORY DZ",..: 17 17 7 13 1 14 17 1 17 17 ...
 $ READMIT.DRG         : Factor w/ 141 levels "100","102","103",..: 19 19 65 127 41 80 18 42 20 21 ...
 $ READMIT.DX.CLASF    : Factor w/ 195 levels "ABDOMINAL ANEURYSM WITHOUT MENTION OF RUPTURE",..: 126 125 189 117 16 116 51 15 126 161 ...
 $ READMIT.ADMITTING.DR: Factor w/ 59 levels (intentionally blank)
 $ READMIT.ATTENDING.DR: Factor w/ 59 levels (intentionally blank)
 $ READMIT.HOSP.SVC    : Factor w/ 8 levels "CCU","MED","MIC",..: 2 2 2 5 2 2 2 2 2 2 ...

根据该数据,我感兴趣的只有两(2)个READMIT.SOURCE ID,"Emergency Unit""Transfer from Long Term Care",原因是798个观察结果中有两个占其中的791个。

起初我尝试subset()这是一个没有beuno。所以现在我做了以下事情:

er <- data.frame(radata[radata$READMIT.SOURCE == "Emergency Unit",])
lt <- data.frame(radata[radata$READMIT.SOURCE == "Transfer from Long Term Care",])

以上两行给了我我需要的东西。我现在的问题是,如何创建一个新的data.frame,它将包含两个新创建的? erlt的结构与上面显示的结构完全相同。

谢谢,

1 个答案:

答案 0 :(得分:3)

请阅读R的更多教程和介绍,并特别注意子集化。您正在寻找%in%

radata[radata$READMIT.SOURCE %in% c("Emergency Unit", "Transfer from Long Term Care"),]

当然,您也可以使用两个data.frames并在其上使用rbind