Return Row Index Number by Variable

Hi!

I am trying to return the row index number for the variable "Grand" in this dataframe:

rob <- structure(list(TRIBUTARY = c("St Louis", "Bad", "Ontonagon",
"Ford", "Menominee", "Fox", "Manitowoc", "Milwaukee", "Indiana Canal",
"Burns Ditch", "St. Joseph", "Kalamazoo", "Grand", "Muskegon",
"AuSable", "Rifle", "Saginaw", "Clinton", "Rouge", "Huron", "Raisin",
"Maumee", "Portage", "Vermilion", "Black", "Cuyahoga", "Cattaraugus",
"Genesee", "Oswego", "St. Regis", NA, NA, "MEAN_SUMMARY_BY_LAKE",
"Superior", "Michigan", "Huron", "Erie", "Ontario"), USGS_STATION_NO = c(4024000,
4027000, 4040000, 4059500, 4067500, 40851385, 4085427, 4087170,
4092750, 4095090, 4101500, 4108660, 4119400, 4121970, 4137500,
4142000, 4157000, 4165500, 4166500, 4174500, 4176500, 4193500,
4195500, 4199500, 4200500, 4208000, 4213500, 4231600, 4249000,
4269000, NA, NA, NA, NA, NA, NA, NA, NA), %_AGRICULTURE = c(2.9,
5.8, 3.5, 3.6, 3.7, 41.4, 69.9, 43.2, 1, 27.4, 60.4, 48.7, 54,
19.5, 2.1, 16.8, 44.5, 19.5, 0.1, 25.2, 67.3, 78.7, 84.2, 65.6,
56.2, 17.5, 33.9, 45.3, 39.5, 2.7, NA, NA, NA, NA, NA, NA, NA,
NA), Mean_Load_TP(tonnes/yr) = c("166", "184", "326", "4",
"76", "544", "93", "79", "91", "45", "263", "132", "430", "48",
"11", "28", "491", "123", "20", "23", "198", "2600", "177", "156",
"191", "313", "Bp", "333", "289", "22", NA, NA, NA, "225", "171",
"177", "422", "215"), Mean_Yield_TP(tonnes/km2/yr) = c("18.7",
"119", "93.9", "3.1", "7.5", "33.200000000000003", "68.099999999999994",
"35.200000000000003", "910", "52.7", "27.7", "26.2", "31.4",
"7.9", "2.5", "33.5", "31.3", "64.7", "42", "12.1", "73.400000000000006",
"158", "160", "230", "186", "171", "Bp", "52", "21.9", "13.8",
NA, NA, NA, "77.2", "29.3", "22.4", "122", "29.2"), Mean_Flow(cfs) = c(2460,
626, 1140, 227, 2310, 4740, 415, 886, 823, 419, 3540, 2070, 4770,
2140, 1290, 363, 5170, 657, 148, 560, 846, 6140, 494, 328, 526,
1160, 737, 2600, 6470, 1030, NA, NA, NA, 1410, 2152, 2280, 1160,
3370), Mean_TP(mg/L) = c(0.059, 0.282, 0.144, 0.015, 0.042,
0.151, 0.294, 0.102, 0.134, 0.133, 0.109, 0.073, 0.111, 0.026,
0.009, 0.113, 0.118, 0.282, 0.18, 0.059, 0.231, 0.358, 0.352,
0.283, 0.272, 0.328, 0.341, 0.157, 0.048, 0.025, NA, NA, NA,
0.162, 0.108, 0.08, 0.269, 0.077), Median_TP(mg/L) = c(0.047,
0.142, 0.05, 0.012, 0.032, 0.123, 0.27, 0.092, 0.118, 0.109,
0.077, 0.069, 0.102, 0.023, 0.009, 0.078, 0.099, 0.212, 0.147,
0.037, 0.197, 0.276, 0.334, 0.177, 0.224, 0.215, 0.125, 0.1,
0.047, 0.021, NA, NA, NA, 0.05, 0.092, 0.078, 0.205, 0.047)), row.names = c(NA,
-38L), class = c("tbl_df", "tbl", "data.frame"))

I have tried the following code:
which(rownames(rob) == "Grand"), but the output I receive is integer(0). Which is not what I want.

Can anyone tell me what I'm doing wrong?

Thank you sooooo much!

I cannot copy and paste the data.frame so it is not exactly reproducible. But from reading the dput, it appears as though the row.names are a 38 length NA. Recognizing this, perhaps

which(rob$TRIBUTARY == "Grand")

Hmmm... is there a way for me to make it copy and pasteable?

Variable names with characters that do not require escaping and the like would be my guess.

Google, "r character that require escape"

I noticed that you have column names containing special characters which R doesn't allow: %_AGRICULTURE, tonnes/yr, Mean_TP(mg/L), etc.

You can circumvent it by using so-called backticks, but it would be much better to not use them at all if possible. One example of a possible renaming would be: Mean_TP(mg/L) > Median_TP_mg_L

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.