Problem with for loop: arguments imply differing number of rows: 0, 1

Hello all,

I have the code below:

vectorstates <- unique(vectorstates)
    for (x in vectorstates){
      filtered <- filter(removal, removal$State %in% x)
      datafiltered <- filtered[order(filtered[,'Pneumonia],filtered[,’Hospital.Name’]),]
      hospital <- filter(datafiltered, datafiltered[,'Pneumonia] %in% datafiltered[3,'Pneumonia'])
      hospital <- data.frame(hospital$Hospital.Name)
      ID <- data.frame(x)
      Collected <- cbind(hospital,ID)
      Emptydataframe <- rbind(Emptydataframe,Collected)
    }
    print(Emptydataframe)

I am trying to create a dataframe that goes through each character (State in the USA) of the vector, vectorstates, and then finds the name of the hospital (Hospital.Name), that corresponds to the lowest value of the column "Pneumonia" for that particular state.

My object vectorstates, looks like this:

vectorstates <- [1] "AL" "AK" "AZ" "AR" "CA" "CO" "CT" "DE" "DC" "FL" "GA" "HI" "ID" "IL"
[15] "IN" "IA" "KS" "KY" "LA" "ME" "MD" "MA" "MI" "MN" "MS" "MO" "MT" "NE"
[29] "NV" "NH" "NJ" "NM" "NY" "NC" "ND" "OH" "OK" "OR" "PA" "PR" "RI" "SC"
[43] "SD" "TN" "TX" "UT" "VT" "VI" "VA" "WA" "WV" "WI" "WY" "GU"

Everytime I run this code, I get the error:

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 0, 1

Would anybody be able to give me a helping hand as to why I am getting this error?

structure(list(Target = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("70.1075", 
"101.71", "149.1155", "220.2225", "326.8835"), class = "factor"), 
    Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("Beaconed-0", 
    "Beaconed-0.5", "Beaconed-2", "Probe-0", "Probe-0.5", "Probe-2"
    ), class = "factor"), ID = c("P_200214103155", "P_200214123342", 
    "P_200217101213", "P_200219091823", "P_200219102411", "P_200219111605", 
    "P_200219123120", "P_200219163622", "P_200219175102", "P_200220130332", 
    "P_200220145557", "P_200221101028", "P_200224092247", "P_200225171612", 
    "P_200225184226", "P_200214103155", "P_200214123342", "P_200217101213", 
    "P_200219091823", "P_200219102411", "P_200219111605", "P_200219123120", 
    "P_200219163622", "P_200219175102", "P_200220130332", "P_200220145557", 
    "P_200221101028", "P_200224092247", "P_200225171612", "P_200225184226", 
    "P_200214103155", "P_200214123342", "P_200217101213", "P_200219091823", 
    "P_200219102411", "P_200219111605", "P_200219123120", "P_200219163622", 
    "P_200219175102", "P_200220130332", "P_200220145557", "P_200221101028", 
    "P_200224092247", "P_200225171612", "P_200225184226", "P_200214103155", 
    "P_200214123342", "P_200217101213", "P_200219091823", "P_200219102411"
    ), Absolutefirststoperror = c(5.8022, 8.6136, 7.7334, 3.3824, 
    4.1562, 8.3978, 6.7964, 5.8266, 5.0725, 4.153, 5.977, 4.5832, 
    7.57525, 4.66425, 7.55, 6.7488, 7.8048, 7.5708, 7.2012, 4.3314, 
    8.2928, 5.1, 3.0236, 4.7482, 5.1454, 3.8708, 5.6708, 6.7938, 
    4.4986, 6.9404, 5.7226, 6.5688, 6.9756, 4.3906, 13.908, 13.6114, 
    4.8192, 4.7482, 3.2736, 2.5638, 6.602, 3.7658, 9.3858, 8.9644, 
    8.5324, 14.7138, 10.7642, 9.6718, 16.383, 5.9138), transform = c(1.75823715597722, 
    2.15334234951937, 2.04554861065665, 1.21858551669378, 1.42460119527448, 
    2.12796976678432, 1.91639306022947, 1.76243363984623, 1.62383379271783, 
    1.42383096464862, 1.78791876984212, 1.52239744411223, 2.02488635415117, 
    1.53992704966997, 2.02154756326093, 1.90936471130232, 2.05473892903946, 
    2.02429874218263, 1.97424767880133, 1.46589081539103, 2.1153876684533, 
    1.62924053973028, 1.10644817443588, 1.55776559885943, 1.63810311164787, 
    1.35346120402465, 1.73533020126134, 1.91601043156591, 1.50376623726006, 
    1.93735940974538, 1.74442324757176, 1.88233116741244, 1.94241834551317, 
    1.47946589200769, 2.63246421414561, 2.61090767691312, 1.57260793918338, 
    1.55776559885943, 1.18589029677517, 0.941490532895897, 1.887372633431, 
    1.32596032208019, 2.23919790881364, 2.1932611778793, 2.14387068183553, 
    2.68878582892075, 2.37622581314541, 2.26921443485221, 2.79624421183813, 
    1.7772886027996), transform2 = c(2.40877562259335, 2.9348935244741, 
    2.78089913517193, 1.83913022921162, 2.03867604096384, 2.89789578832642, 
    2.60699060220784, 2.41383512278697, 2.25222112591104, 2.03789106676486, 
    2.44479037956222, 2.14084095625995, 2.75231720555607, 2.15968747739112, 
    2.74772633280682, 2.59784526098072, 2.79370721443748, 2.75150867707154, 
    2.68350517048132, 2.08120157601324, 2.87972220882501, 2.25831795812724, 
    1.73885019481265, 2.17903648432054, 2.26834741607189, 1.96743487821071, 
    2.38134415824341, 2.60649189524924, 2.12099033472574, 2.6344638923318, 
    2.39219564417294, 2.5629670306112, 2.64113611917296, 2.09537586127167, 
    3.72934310569569, 3.68936308866449, 2.19526763744196, 2.17903648432054, 
    1.80930926046378, 1.60118705965293, 2.56943573572098, 1.94056692747248, 
    3.06362530345994, 2.99406078762606, 2.92102721657981, 3.83585714019696, 
    3.28088402720974, 3.10995176811474, 4.0475918766595, 2.43183058620456
    )), row.names = c(NA, -50L), class = "data.frame")

Hi @eyavuz21: Having you tried executing the loop by hand -- picking a value for x, then executing each command and inspecting its result?

Hi @dromano yes when I run it line by line and substitute values, it works! But not in the context of the loop/function.

Could you add the line print(x) at the beginning of the loop, run it, and seen when the error is triggered?

vectorstates <- unique(vectorstates)
vectorstates <- data.frame(vectorstates)
for (x in vectorstates){
print(x)
filtered <- filter(removal, removal$State %in% x)
datafiltered <- filtered[order(filtered[,y],filtered[,'Hospital.Name']),]
hospital <- filter(datafiltered, datafiltered[,y] %in% datafiltered[z,y])
printdata <- data.frame(print(hospital$Hospital.Name))
ID <- data.frame(x)
Collected <- cbind(printdata,ID)
Emptydataframe <- rbind(Emptydataframe,Collected)
}
print(Emptydataframe)
}}

when I try adding the line vectorstates <- data.frame(vectorstates); it works better, and this is the output:

Rankall('Pneumonia',5)
 [1] AL AK AZ AR CA CO CT DE DC FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN
[25] MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA PR RI SC SD TN TX UT VT VI
[49] VA WA WV WI WY GU
54 Levels: AK AL AR AZ CA CO CT DC DE FL GA GU HI IA ID IL IN KS KY ... WY
[1] "HUNTINGTON MEMORIAL HOSPITAL"                      
[2] "UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER"
                        print.hospital.Hospital.Name.  x
1                        HUNTINGTON MEMORIAL HOSPITAL AL
2  UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER AK
3                        HUNTINGTON MEMORIAL HOSPITAL AZ
4  UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER AR
5                        HUNTINGTON MEMORIAL HOSPITAL CA
6  UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER CO
7                        HUNTINGTON MEMORIAL HOSPITAL CT
8  UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER DE
9                        HUNTINGTON MEMORIAL HOSPITAL DC
10 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER FL
11                       HUNTINGTON MEMORIAL HOSPITAL GA
12 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER HI
13                       HUNTINGTON MEMORIAL HOSPITAL ID
14 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER IL
15                       HUNTINGTON MEMORIAL HOSPITAL IN
16 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER IA
17                       HUNTINGTON MEMORIAL HOSPITAL KS
18 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER KY
19                       HUNTINGTON MEMORIAL HOSPITAL LA
20 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER ME
21                       HUNTINGTON MEMORIAL HOSPITAL MD
22 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER MA
23                       HUNTINGTON MEMORIAL HOSPITAL MI
24 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER MN
25                       HUNTINGTON MEMORIAL HOSPITAL MS
26 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER MO
27                       HUNTINGTON MEMORIAL HOSPITAL MT
28 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER NE
29                       HUNTINGTON MEMORIAL HOSPITAL NV
30 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER NH
31                       HUNTINGTON MEMORIAL HOSPITAL NJ
32 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER NM
33                       HUNTINGTON MEMORIAL HOSPITAL NY
34 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER NC
35                       HUNTINGTON MEMORIAL HOSPITAL ND
36 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER OH
37                       HUNTINGTON MEMORIAL HOSPITAL OK
38 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER OR
39                       HUNTINGTON MEMORIAL HOSPITAL PA
40 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER PR
41                       HUNTINGTON MEMORIAL HOSPITAL RI
42 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER SC
43                       HUNTINGTON MEMORIAL HOSPITAL SD
44 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER TN
45                       HUNTINGTON MEMORIAL HOSPITAL TX
46 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER UT
47                       HUNTINGTON MEMORIAL HOSPITAL VT
48 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER VI
49                       HUNTINGTON MEMORIAL HOSPITAL VA
50 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER WA
51                       HUNTINGTON MEMORIAL HOSPITAL WV
52 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER WI
53                       HUNTINGTON MEMORIAL HOSPITAL WY
54 UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER GU

However, there are only two hospitals selected and I feel there should be more...

So are there no errors now?

yes there are no errors appearing but do you agree that this command should print out a data frame, where for each state, it prints the hospital with the nth lowest morality rate from 'Pneumonia'. Given that I have ordered the variable 'Pneumonia' (being the mortality rate from Pneumonia in ascending order...

for (x in vectorstates){
print(x)
filtered <- filter(removal, removal$State %in% x)
datafiltered <- filtered[order(filtered[,y],filtered[,'Hospital.Name']),]
hospital <- filter(datafiltered, datafiltered[,y] %in% datafiltered[z,y])
printdata <- data.frame(print(hospital$Hospital.Name))
ID <- data.frame(x)
Collected <- cbind(printdata,ID)
Emptydataframe <- rbind(Emptydataframe,Collected)
}
print(Emptydataframe)
}}

so the n is specified by the value z
z = 2 means 2nd lowest

Quick note: Since you changed vectorstates to a data frame, you shouldn't need a for loop.

Otherwise, it's difficult to know what's going on since you didn't supply self-contained data and code.

but I want to go through each state in 'vector states' and find the hospital with the lowest mortality rate from 'pneumonia' in that state.

The is only one object in vectorstates once you changed it to a data frame -- its only column.

yes I want to through each row of that column, each row being a state

It should already do that -- try it with and without the loop and see if you get the same result.

vectorstates <- unique(vectorstates)
for (x in vectorstates){
filtered <- filter(removal, removal$State %in% x)
datafiltered <- filtered[order(filtered[,y],filtered[,'Hospital.Name']),]
hospital <- filter(datafiltered, datafiltered[,y] %in% datafiltered[z,y])
printdata <- data.frame(print(hospital$Hospital.Name))
ID <- data.frame(x)
Collected <- cbind(printdata,ID)
Emptydataframe <- rbind(Emptydataframe,Collected)
}
print(Emptydataframe)
}}

like this?

I meant this, but again, it's difficult to tell sine you didn't post self-contained data and code:

x <- data.frame(vectorstates)
print(x)
filtered <- filter(removal, removal$State %in% x)
datafiltered <- filtered[order(filtered[,y],filtered[,'Hospital.Name']),]
hospital <- filter(datafiltered, datafiltered[,y] %in% datafiltered[z,y])
printdata <- data.frame(print(hospital$Hospital.Name))
ID <- data.frame(x)
Collected <- cbind(printdata,ID)
print(Collected)

There is an important point I want to make. code works fine here: when I enter Z as 'best' or 'worst', but not when I enter a numeric value for Z (excluding the lowest and highest (best and worst)).

Emptydataframe = NULL
Rankall <- function(y, z){
if (z == 'best'){ z <- 1 
removal <- outcomemeasures[!is.na(outcomemeasures[y]),]
vectorstates <- as.vector(outcomemeasures[,'State'])
vectorstates <- unique(vectorstates)
vectorstates <- data.frame(vectorstates)
for (x in vectorstates){
filtered <- filter(removal, removal$State %in% x)
datafiltered <- filtered[order(filtered[,y],filtered[,'Hospital.Name']),]
hospital <- filter(datafiltered, datafiltered[,y] %in% datafiltered[z,y])
printdata <- data.frame(hospital$Hospital.Name)
ID <- data.frame(x)
Collected <- cbind(printdata,ID)
Emptydataframe <- rbind(Emptydataframe,Collected)
  }
print(Emptydataframe)
 }
else if (z == 'worst'){
removal <- outcomemeasures[!is.na(outcomemeasures[y]),]
vectorstates <- as.vector(outcomemeasures[,'State'])
vectorstates <- unique(vectorstates)
vectorstates <- data.frame(vectorstates)
for (x in vectorstates){
filtered <- filter(removal, removal$State %in% x)
datafiltered <- filtered[order(filtered[,y],filtered[,'Hospital.Name']),]
j <- datafiltered 
z <- nrow(j) 
hospital <- filter(datafiltered, datafiltered[,y] %in% datafiltered[z,y])
printdata <- data.frame(hospital$Hospital.Name)
ID <- data.frame(x)
Collected <- cbind(printdata,ID)
Emptydataframe <- rbind(Emptydataframe,Collected)
}
print(Emptydataframe)
}
else if (z != 'best' && z != 'worst'){
removal <- outcomemeasures[!is.na(outcomemeasures[y]),]
vectorstates <- as.vector(outcomemeasures[,'State'])
vectorstates <- unique(vectorstates)
x <- data.frame(vectorstates)
print(x)
filtered <- filter(removal, removal$State %in% x)
datafiltered <- filtered[order(filtered[,y],filtered[,'Hospital.Name']),]
hospital <- filter(datafiltered, datafiltered[,y] %in% datafiltered[z,y])
printdata <- data.frame(print(hospital$Hospital.Name))
ID <- data.frame(x)
Collected <- cbind(printdata,ID)
Emptydataframe <- rbind(Emptydataframe,Collected)
}
print(Emptydataframe)
}

@dromano

This is still incomplete, so not testable. I have a request, which I hope you'll follow exactly: Rather than attempt a function, could you give an example of all the steps applied to a single state, with all the data needed to execute those steps? You should also run the steps first yourself to make sure they do or don't work., and note where they don't.

@eyavuz21: Could you do this for the numeric case? (And I'd ask that you make new posts rather than repeatedly editing a previous one, so it's clearer for folks to follow.)

@ David you mean explaining line by line from this line onwards: filtered <- filter(removal, removal$State %in% x)

what I did etc..

removal <- outcomemeasures[!is.na(outcomemeasures[y]),]; here I am removing NA values from the outcomemeasures data set
vectorstates <- as.vector(outcomemeasures[,'State']); extracting values from the column 'State' and storing them as a vector
vectorstates <- unique(vectorstates); making sure each state is mentioned only once in the vector

For each state (#1) I want to filter out the rows which correspond to that particular state from the dataframe 'removal' (#2). I then want to sort this data frame by ordering the rows by 'Pneumonia' and then by 'Hospital name' (#3). I then want to filter out the zth row of the column 'Pneumonia' (#4). I then want to print out the hospital name that corresponds to this row (#5) and assign this new dataframe 'printdata'. I then want to assign a dataframe 'ID' which contains the list of states (#6). I then want to create a dataframe 'collected', where I bind the columns from the dataframe ID and the dataframe printdata (#7). I then want to bind the emptydataframe to the dataframe 'collected', calling this new dataframe 'Emptydataframe' (#8). I then want to print this dataframe (#9).

for (x in vectorstates){ #1
filtered <- filter(removal, removal$State %in% x) #2
datafiltered <- filtered[order(filtered[,y],filtered[,'Hospital.Name']),] #3
hospital <- filter(datafiltered, datafiltered[,y] %in% datafiltered[z,y]) #4
printdata <- data.frame(print(hospital$Hospital.Name)) #5
ID <- data.frame(x) #6
Collected <- cbind(printdata,ID) #7
Emptydataframe <- rbind(Emptydataframe,Collected) #8
}
print(Emptydataframe) #9
}}

Could you run your original code starting from removal <- ..., with a specific value for x?

> removal <- outcomemeasures[!is.na(outcomemeasures[y]),]
> vectorstates <- as.vector(outcomemeasures[,'State'])
> vectorstates <- unique(vectorstates)
> filtered <- filter(removal, removal$State %in% 'CA')
>     datafiltered <- filtered[order(filtered[,y],filtered[,'Hospital.Name']),]
>     hospital <- filter(datafiltered, datafiltered[,y] %in% datafiltered[z,y])
>     printdata <- data.frame(print(hospital$Hospital.Name))

[1] "SAINT VINCENT MEDICAL CENTER"

  ID <- data.frame('CA')
>     Collected <- cbind(printdata,ID)
>     Emptydataframe <- rbind(Emptydataframe,Collected)
>     print(Emptydataframe)

print.hospital.Hospital.Name. X.CA.
1 SAINT VINCENT MEDICAL CENTER CA

so the above printout gives me the state and the medical centre, which is exactly what I want, but for every state! @dromano