-1

I have a list of 54 data frames (stateOrdRank). The columns that I'm most interested in are columns 2, 7, and 47 with the names "Hospital.nam", "State", and "rank", respectively. The list is indexed by "State" so the value for "State" is the same in each row. I created a for loop that records the maximum rank in each table, and if an argument "num" is within a certain range, I look into each table and rbind the subset for "Hospital.name" and "State" where the value in rank = num, to the data frame "finalFrame". The range is 1 to the maximum value of rank for each table in the list. The following is the part I can't figure out. If num is outside my range (i.e. the range in each table), I want to rbind to "finalFrame" the value NA in the "Hospital.name" column, but the true "State" value for that table (i.e. the correct "State" value like I was doing earlier). How do I do this? Below is the code I have so far.

finalFrame <- data.frame()
for (i in seq_along(stateOrdRank)) {
    maxRank[i] <- max(stateOrdRank[[47]])
    if (num <= 1 && num <= maxRank[i]) {
            finalFrame <- rbind(finalFrame, subset(stateOrdRank[i], stateOrdRank[[i]] == num,   select = c("Hospital.Name", "State"))))
    } else {
            finalFrame <- rbind(finalFrame,  
    }
}

Here's a sample look at half of the fist element of stateOrdRank and about 0.25 of the second element.

$AK
                                       Hospital.Name State rank
115                         SOUTH PENINSULA HOSPITAL    AK    1
104               YUKON KUSKOKWIM DELTA REG HOSPITAL    AK    2
100                   MAT-SU REGIONAL MEDICAL CENTER    AK    3
114 PEACEHEALTH KETCHIKAN MEDICAL             CENTER    AK    4
101                       BARTLETT REGIONAL HOSPITAL    AK    5
105               CENTRAL PENINSULA GENERAL HOSPITAL    AK    6
106                     ALASKA NATIVE MEDICAL CENTER    AK    7
99                  PROVIDENCE ALASKA MEDICAL CENTER    AK    8
103                         ALASKA REGIONAL HOSPITAL    AK    9


$AL
                                       Hospital.Name State rank
23                       ANDALUSIA REGIONAL HOSPITAL    AL    1
15                     JACKSON HOSPITAL & CLINIC INC    AL    2
19                                  WEDOWEE HOSPITAL    AL    3
44                         FLORALA MEMORIAL HOSPITAL    AL    4
55                      GROVE HILL MEMORIAL HOSPITAL    AL    5
82                         SPRINGHILL MEDICAL CENTER    AL    6
86                      BIRMINGHAM VA MEDICAL CENTER    AL    7
36                            PARKWAY MEDICAL CENTER    AL    8
3442
  • 8,248
  • 2
  • 19
  • 41
uwayor
  • 1
  • 4
  • So where does the correct "State" value come from? Without the a representative `stateOrdRank` object include in the example, it's hard to tell how this is supposed to work. Please read [how to create a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – MrFlick Nov 02 '14 at 23:50
  • I added a sample of stateOrdRank showing the columns I'm talking about. – uwayor Nov 03 '14 at 00:07

1 Answers1

0

How about

finalFrame <- rbind(finalFrame,  list("Hospital.Name"=NA, "State"=names(stateOrdRank)[i]))

(Untested since a reproducible example was not provided.)

MrFlick
  • 195,160
  • 17
  • 277
  • 295