在R中应用'apply'函数

时间:2014-01-23 22:45:41

标签: r sapply

我有以下列表,它是使用split函数生成的,状态为索引。

 $AK
                   Hospital_Name         State     Mortality_Rate
  99  PROVIDENCE ALASKA MEDICAL CENTER    AK           13.4
 100   MAT-SU REGIONAL MEDICAL CENTER    AK           17.7
 102      FAIRBANKS MEMORIAL HOSPITAL    AK           15.5

 $AL
                                 Hospital_Name        State   Mortality_Rate
  1                 SOUTHEAST ALABAMA MEDICAL CENTER    AL           14.3
  2                    MARSHALL MEDICAL CENTER SOUTH    AL           18.5
  3                   ELIZA COFFEE MEMORIAL HOSPITAL    AL           18.1


$AR
                             Hospital_Name       State   Mortality_Rate
 193           SILOAM SPRINGS MEMORIAL HOSPITAL    AR           15.6
 194            JOHNSON REGIONAL MEDICAL CENTER    AR           16.9
 195 WASHINGTON REGIONAL MED CTR AT NORTH HILLS    AR           15.2

我想从每个状态中选择第二行的医院名称。有人可以在这里帮助申请功能吗?我试图通过以下方式使用sapply(不工作) -

       x <- sapply(test.case3,function(i) test.case3[[i]][2,1])

这样,通过改变'i',我可以得到如下结果 -

   > test.case3[[1]][2,1]
    [1] "MAT-SU REGIONAL MEDICAL CENTER"
   > test.case3[[2]][2,1]
    [1] "MARSHALL MEDICAL CENTER SOUTH"
   > test.case3[[3]][2,1]
    [1] "JOHNSON REGIONAL MEDICAL CENTER"

请告知。

我需要按以下格式制作最终报告。(忽略数据,在下面的例子中)

       Hospital_Name                    State
    D W MCMILLAN MEMORIAL HOSPITAL       AL
   ARKANSAS METHODIST MEDICAL CENTER     AR
   JOHN C LINCOLN DEER VALLEY HOSPITAL   AZ

0 个答案:

没有答案