Trouble with For Loop

Hello! Thank you so much to those who are reading and willing to help.

I have created a series of functions for analysis of a dataset. The functions create a subset of data by site, graph the data, and then write the data to an appending .csv file. Instead of manually plugging in the site numbers, however, I want it to loop the function through a series of site numbers. However, when I try to do it, it just keeps repeating the same site number over and over. Can anyone help me!?

Here are the two datasets I'm using (the wq is a subset because it's 2000 observations long):

wq <- structure(list(PUBLICATION_DATE = c(42933, 42933, 42933, 42933, 
42933, 42933, 42933, 42933, 42933, 42933, 42933, 42933, 42933, 
42933, 42933, 42933, 42933, 42933, 42933, 42933, 42933, 42933, 
42933, 42933, 42933, 42933, 42933, 42933, 42933, 42933, 42933, 
42933, 42933, 42933, 42933, 42933, 42933, 42933, 42933, 42933, 
42933, 42933, 42933, 42933, 42933, 42933, 42933, 42933, 42933, 
42933), UID = c(169770, 173491, 169770, 173491, 169770, 173491, 
169770, 173491, 169770, 173491, 169770, 173491, 169770, 173491, 
169770, 173491, 169770, 173491, 169770, 173491, 169770, 173491, 
169770, 173491, 169770, 173491, 169770, 173491, 169770, 173491, 
169770, 173491, 169770, 173491, 169011, 169011, 169011, 169011, 
169011, 169011, 169011, 169542, 169542, 169542, 169542, 169542, 
169542, 169542, 169542, 169542), SITE_ID = c("GLBA15-1013", "GLBA15-1013", 
"GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", 
"GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", 
"GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", 
"GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", 
"GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", 
"GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", "GLBA15-1013", 
"GLBA15-1013", "GLBA15-1013", "GLBA15-1034", "GLBA15-1034", "GLBA15-1034", 
"GLBA15-1034", "GLBA15-1034", "GLBA15-1034", "GLBA15-1034", "GLBA15-1052", 
"GLBA15-1052", "GLBA15-1052", "GLBA15-1052", "GLBA15-1052", "GLBA15-1052", 
"GLBA15-1052", "GLBA15-1052", "GLBA15-1052"), DATE_COL = c(42179, 
42207, 42179, 42207, 42179, 42207, 42179, 42207, 42179, 42207, 
42179, 42207, 42179, 42207, 42179, 42207, 42179, 42207, 42179, 
42207, 42179, 42207, 42179, 42207, 42179, 42207, 42179, 42207, 
42179, 42207, 42179, 42207, 42179, 42207, 42172, 42172, 42172, 
42172, 42172, 42172, 42172, 42177, 42177, 42177, 42177, 42177, 
42177, 42177, 42177, 42177), VISIT_NO = c(1, 2, 1, 2, 1, 2, 1, 
2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 
1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1), STUDY = c("NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", 
"NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", 
"NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", 
"NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", 
"NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", 
"NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", 
"NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", 
"NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", 
"NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", 
"NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", "NCCA 2015", 
"NCCA 2015"), PSTL_CODE = c("PA", "PA", "PA", "PA", "PA", "PA", 
"PA", "PA", "PA", "PA", "PA", "PA", "PA", "PA", "PA", "PA", "PA", 
"PA", "PA", "PA", "PA", "PA", "PA", "PA", "PA", "PA", "PA", "PA", 
"PA", "PA", "PA", "PA", "PA", "PA", "MI", "MI", "MI", "MI", "MI", 
"MI", "MI", "PA", "PA", "PA", "PA", "PA", "PA", "PA", "PA", "PA"
), SAMPLE_TYPE = c("HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", 
"HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", 
"HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", 
"HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", 
"HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", 
"HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", 
"HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", "HYDRO", 
"HYDRO", "HYDRO", "HYDRO"), LINE = c(1, 1, 2, 2, 3, 3, 4, 4, 
5, 5, 6, 6, 7, 7, 8, 8, 16, 24, 15, 23, 14, 22, 13, 21, 12, 20, 
11, 19, 10, 18, 9, 17, 0, 0, 1, 2, 3, 6, 5, 4, 0, 1, 2, 3, 4, 
5, 21, 20, 19, 18), CAST = c("DOWNCAST", "DOWNCAST", "DOWNCAST", 
"DOWNCAST", "DOWNCAST", "DOWNCAST", "DOWNCAST", "DOWNCAST", "DOWNCAST", 
"DOWNCAST", "DOWNCAST", "DOWNCAST", "DOWNCAST", "DOWNCAST", "DOWNCAST", 
"DOWNCAST", "UPCAST", "UPCAST", "UPCAST", "UPCAST", "UPCAST", 
"UPCAST", "UPCAST", "UPCAST", "UPCAST", "UPCAST", "UPCAST", "UPCAST", 
"UPCAST", "UPCAST", "UPCAST", "UPCAST", NA, NA, "DOWNCAST", "DOWNCAST", 
"DOWNCAST", "UPCAST", "UPCAST", "UPCAST", NA, "DOWNCAST", "DOWNCAST", 
"DOWNCAST", "DOWNCAST", "DOWNCAST", "UPCAST", "UPCAST", "UPCAST", 
"UPCAST"), COL_LOC = c("Surface", "Surface", "Mid-water", "Mid-water", 
"Mid-water", "Mid-water", "Mid-water", "Mid-water", "Mid-water", 
"Mid-water", "Mid-water", "Mid-water", "Mid-water", "Mid-water", 
"Bottom", "Bottom", "Surface", "Surface", "Mid-water", "Mid-water", 
"Mid-water", "Mid-water", "Mid-water", "Mid-water", "Mid-water", 
"Mid-water", "Mid-water", "Mid-water", "Mid-water", "Mid-water", 
"Bottom", "Bottom", NA, NA, "Surface", "Mid-water", "Bottom", 
"Surface", "Mid-water", "Bottom", NA, "Surface", "Mid-water", 
"Mid-water", "Mid-water", "Bottom", "Surface", "Mid-water", "Mid-water", 
"Mid-water"), DEPTH = c(0.1, 0.1, 0.5, 0.5, 1, 1, 2, 2, 3, 3, 
4, 4, 5, 5, 5.2, 5.3, 0.1, 0.1, 0.5, 0.5, 1, 1, 2, 2, 3, 3, 4, 
4, 5, 5, 5.2, 5.3, NA, NA, 0.1, 0.5, 0.8, 0.1, 0.5, 0.8, NA, 
0.1, 0.5, 1, 2, 2.8, 0.1, 0.5, 1, 2), CONDUCTIVITY = c(330, 297.8, 
330.2, 297.8, 329.8, 297.8, 329.9, 297.7, 328.7, 297.7, 328, 
297.7, 327.3, 297.8, 324.4, 297.8, 325.7, 297.9, 325.9, 297.9, 
325.8, 297.9, 325, 297.8, 324.8, 297.7, 325.2, 297.7, 323.2, 
305.8, 324.3, 297.4, NA, NA, 708, 715, 733, 725, 735, 737, NA, 
303.3, 302.9, 302.8, 302.9, 301.1, 302.9, 302.9, 303, 302.6), 
    DO = c(9.17, 9.99, 9.16, 10.05, 9.19, 10.06, 9.3, 10.02, 
    9.01, 10.04, 8.93, 9.99, 8.67, 9.6, 8.1, 5.44, 9.19, 10.09, 
    9.19, 10.09, 9.19, 10.06, 9.19, 10.01, 8.84, 9.96, 8.63, 
    9.79, 8.12, 8.5, 8.05, 5.4, NA, NA, 7.3, 7.3, 7.35, 7.36, 
    7.4, 7.43, NA, 9.09, 9.21, 9.28, 9.32, 10.03, 9.16, 9.17, 
    9.26, 9.36), LIGHT_AMB = c(1258, 1661, 1008, 1602, 1355, 
    1603, 1663, 1621, 1745, 1698, 1280, 1716, 1316, 1701, 1500, 
    1647, 1789, 1631, 1763, 1662, 1509, 1670, 1356, 1669, 1482, 
    1647, 1330, 1625, 1298, 1605, 1043, 1610, NA, NA, 264.3, 
    280.8, 291, 334.3, 317, 307.2, NA, 1548, 1621, 1670, 1586, 
    1628, 1633, 1662, 1647, 1610), LIGHT_UW = c(259, 845.1, 209.8, 
    470.4, 186.2, 178.4, 197.8, 218.2, 173.3, 98.6, 68.8, 42, 
    28.5, 15.7, 23.4, 8.9, 210.8, 843.7, 205.9, 820, 177.8, 461.8, 
    101, 226.8, 61.8, 110.8, 58.5, 40.8, 33.2, 10.6, 23.5, 8.6, 
    NA, NA, 111.2, 13.35, 4.85, 106.7, 12.89, 4.193, NA, 171.4, 
    140.4, 153.9, 134, 97.76, 195.5, 144.5, 137.2, 124.2), NCCA_FLAG = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), PH = c(8.3, 8.7, 8.3, 8.8, 8.3, 8.8, 8.3, 
    8.7, 8.3, 8.8, 8.3, 8.8, 8.2, 8.7, 8.1, 7.9, 8.3, 8.8, 8.3, 
    8.8, 8.3, 8.8, 8.3, 8.8, 8.2, 8.8, 8.2, 8.7, 8.1, 8.4, 8.1, 
    7.8, NA, NA, 8, 8, 8, 8, 8, 8, NA, 8.2, 8.2, 8.2, 8.2, 8.3, 
    8.2, 8.2, 8.2, 8.2), SALINITY = c(NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
    STATION_DEPTH = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 5.7, 5.8, NA, NA, NA, NA, NA, 
    NA, 1.2, NA, NA, NA, NA, NA, NA, NA, NA, NA), TEMPERATURE = c(21.3, 
    23.6, 21.4, 23.6, 21.2, 23.6, 21.1, 23.6, 21, 23.5, 21, 23.5, 
    21, 23.3, 20.7, 20.3, 21.4, 23.6, 21.4, 23.6, 21.4, 23.6, 
    21.1, 23.6, 21, 23.5, 21, 23.5, 20.7, 21.8, 20.7, 20.3, NA, 
    NA, 23.5, 23.5, 23.5, 23.5, 23.4, 23.4, NA, 20.9, 20.6, 19.9, 
    19.5, 19.1, 21, 20.8, 20.1, 19.5), NCCA_COMMENT = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), WATERBODY = c("Lake_Erie", "Lake_Erie", "Lake_Erie", 
    "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", 
    "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", 
    "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", 
    "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", 
    "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", 
    "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", 
    "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", 
    "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", 
    "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", "Lake_Erie", 
    "Lake_Erie", "Lake_Erie")), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame"))

sites <- structure(list(SITE_ID_2020 = c("LEA20_MI-10010", "LEA20_MI-10011", 
"LEA20_MI-10012", "LEA20_MI-10013", "LEA20_MI-10014", "LEA20_MI-10015", 
"LEA20_MI-10016", "LEA20_MI-10017", "LEA20_MI-10018", "LEA20_MI-10019", 
"LEA20_NY-10016", "LEA20_NY-10017", "LEA20_NY-10018", "LEA20_NY-10019", 
"LEA20_NY-10020", "LEA20_NY-10021", "LEA20_NY-10022", "LEA20_NY-10023", 
"LEA20_NY-10024", "LEA20_NY-10025", "LEA20_NY-10026", "LEA20_NY-10027", 
"LEA20_NY-10028", "LEA20_NY-10029", "LEA20_NY-10030", "LEA20_NY-10031", 
"LEA20_OH-10041", "LEA20_OH-10042", "LEA20_OH-10043", "LEA20_OH-10044", 
"LEA20_OH-10045", "LEA20_OH-10046", "LEA20_OH-10047", "LEA20_OH-10048", 
"LEA20_OH-10049", "LEA20_OH-10050", "LEA20_OH-10051", "LEA20_OH-10052", 
"LEA20_OH-10053", "LEA20_OH-10054", "LEA20_OH-10055", "LEA20_OH-10056", 
"LEA20_OH-10057", "LEA20_OH-10058", "LEA20_OH-10059", "LEA20_OH-10060", 
"LEA20_OH-10061", "LEA20_OH-10062", "LEA20_OH-10063", "LEA20_OH-10064", 
"LEA20_OH-10065", "LEA20_OH-10066", "LEA20_OH-10067", "LEA20_PA-10005", 
"LEA20_PA-10006", "LEA20_PA-10007", "LEA20_PA-10008", "LEA20_PA-10009", 
"LEA20_PA-10010", "LEA20_PA-10011", "NGL20_MI-10001", "NGL20_MI-10002", 
"NGL20_MI-10003", "NGL20_MI-10004", "NGL20_MI-10005", "NGL20_MI-10006", 
"NGL20_MI-10007", "NGL20_MI-10008", "NGL20_MI-10009", "NGL20_NY-10001", 
"NGL20_NY-10002", "NGL20_NY-10003", "NGL20_NY-10004", "NGL20_NY-10005", 
"NGL20_NY-10006", "NGL20_NY-10007", "NGL20_NY-10008", "NGL20_NY-10009", 
"NGL20_NY-10010", "NGL20_NY-10011", "NGL20_NY-10012", "NGL20_NY-10013", 
"NGL20_NY-10014", "NGL20_NY-10015", "NGL20_OH-10001", "NGL20_OH-10002", 
"NGL20_OH-10003", "NGL20_OH-10004", "NGL20_OH-10005", "NGL20_OH-10006", 
"NGL20_OH-10007", "NGL20_OH-10008", "NGL20_OH-10009", "NGL20_OH-10010", 
"NGL20_OH-10011", "NGL20_OH-10012", "NGL20_OH-10013", "NGL20_OH-10014", 
"NGL20_OH-10015", "NGL20_OH-10016", "NGL20_OH-10017", "NGL20_OH-10018", 
"NGL20_OH-10019", "NGL20_OH-10020", "NGL20_OH-10021", "NGL20_OH-10022", 
"NGL20_OH-10023", "NGL20_OH-10024", "NGL20_OH-10025", "NGL20_OH-10026", 
"NGL20_OH-10027", "NGL20_OH-10028", "NGL20_OH-10029", "NGL20_OH-10030", 
"NGL20_OH-10031", "NGL20_OH-10032", "NGL20_OH-10033", "NGL20_OH-10034", 
"NGL20_OH-10035", "NGL20_OH-10036", "NGL20_OH-10037", "NGL20_OH-10038", 
"NGL20_OH-10039", "NGL20_OH-10040", "NGL20_PA-10001", "NGL20_PA-10002", 
"NGL20_PA-10003", "NGL20_PA-10004"), SITE_ID_2015 = c("GLNS15-2508", 
"GLBA15-3235", "GLNS15-2552", "GLNS15-2532", "GLNS15-2145", "GLNS15-2508", 
"GLNS15-2512", "GLNS15-1164", "GLNS15-2153", "GLNS15-2162", "GLNS15-2165", 
"GLNS15-1155", "GLNS15-2505", "GLNS15-2165", "GLNS15-1155", "GLNS15-1154", 
"GLNS15-2506", "GLNS15-1166", "GLNS15-1158", "GLNS15-1158", "GLNS15-2174", 
"GLNS15-2505", "GLNS15-2506", "GLNS15-2167", "GLNS15-2152", "GLNS15-2506", 
"GLNS15-2161", "GLNS15-2147", "GLNS15-2151", "GLNS15-2155", "GLNS15-1153", 
"GLNS15-1163", "GLNS15-2163", "GLNS15-1171", "GLNS15-2535", "GLNS15-1160", 
"GLNS15-2535", "GLNS15-2516", "GLNS15-2540", "GLNS15-2527", "GLNS15-1160", 
"GLNS15-1152", "GLBA15-3207", "GLNS15-2540", "GLNS15-2527", "GLNS15-1173", 
"GLNS15-2175", "GLNS15-2173", "GLNS15-2144", "GLNS15-1165", "GLNS15-2516", 
"GLNS15-2527", "GLNS15-2516", "GLNS15-2148", "GLNS15-1162", "GLNS15-1162", 
"GLBA15-3239", "GLNS15-2158", "GLNS15-2148", "GLBA15-1013", "GLNS15-1156", 
"GLNS15-1164", "GLNS15-1169", "GLNS15-2532", "GLNS15-2145", "GLNS15-2153", 
"GLNS15-2508", "GLNS15-2153", "GLNS15-2512", "GLNS15-1154", "GLNS15-1155", 
"GLNS15-1158", "GLNS15-1166", "GLNS15-1168", "GLNS15-1170", "GLNS15-2167", 
"GLNS15-2152", "GLNS15-2506", "GLNS15-2165", "GLNS15-1158", "GLNS15-1166", 
"GLNS15-1170", "GLBA15-3219", "GLNS15-2181", "GLNS15-1152", "GLNS15-1153", 
"GLNS15-1157", "GLNS15-1159", "GLNS15-1160", "GLNS15-1161", "GLNS15-1163", 
"GLNS15-1165", "GLNS15-1167", "GLNS15-1171", "GLNS15-1172", "GLNS15-1173", 
"GLNS15-2142", "GLNS15-2144", "GLNS15-2172", "GLNS15-2527", "GLNS15-2155", 
"GLNS15-2176", "GLNS15-1165", "GLNS15-2163", "GLNS15-1163", "GLNS15-2175", 
"GLNS15-1159", "GLNS15-2168", "GLNS15-1167", "GLNS15-2161", "GLNS15-2163", 
"GLNS15-2516", "GLNS15-2173", "GLNS15-2147", "GLNS15-2172", "GLNS15-1159", 
"GLNS15-2176", "GLNS15-2516", "GLNS15-2156", "GLNS15-2163", "GLBA15-3267", 
"GLNS15-2140", "GLNS15-2535", "GLNS15-2154", "GLNS15-1162", "GLNS15-2502", 
"GLNS15-2502", "GLNS15-2158")), row.names = c(NA, -128L), class = c("tbl_df", 
"tbl", "data.frame"))

And here are my functions and the for loop that I'm trying to achieve:

filt <- function(x) {
  filter(wq, SITE_ID == x) %>%
    filter(!is.na(DO)) %>%
    filter(!is.na(TEMPERATURE)) %>%
    filter(!is.na(PH)) %>%
    filter(!is.na(CONDUCTIVITY))
}

all.plots <- function(a,x){
  p1 <- ggplot() + geom_line(data=a, aes(x=DEPTH, y=DO, color=CAST)) + labs(title = "Dissolved Oxygen (mg/L)", subtitle = x) +
    coord_cartesian(xlim=NULL, ylim=c(0,15))
  p2 <- ggplot() + geom_line(data=a, aes(x=DEPTH, y=PH, color=CAST)) + labs(title = "pH", subtitle = x) +
    coord_cartesian(xlim=NULL, ylim=c(0,14))
  p3 <- ggplot() + geom_line(data=a, aes(x=DEPTH, y=TEMPERATURE, color=CAST)) + labs(title = "Temperature (C)", subtitle = x) +
    coord_cartesian(xlim=NULL, ylim=c(0,30))
  p4 <- ggplot() + geom_line(data=a, aes(x=DEPTH, y=CONDUCTIVITY, color=CAST)) + labs(title = "Conductivity (uS)", subtitle = x) +
    coord_cartesian(xlim=NULL, ylim=c(200,1200))
  grid.arrange(p1, p2, p3, p4, ncol=2)
}

means <- function(x,y){
  site.id <- y
  max.depth <- max(x$DEPTH, na.rm=T)
  mean.do <- mean(x$DO, na.rm=T)
  mean.ph <- mean(x$PH, na.rm=T)
  mean.cond <- mean(x$CONDUCTIVITY, na.rm=T)
  mean.temp <- mean(x$TEMPERATURE, na.rm=T)
  result <- cbind(site.id, max.depth, mean.do, mean.ph, mean.cond, mean.temp)
  print(result)
  write.table(result, file = "practice.ERIE_WQ.csv", append=T, col.names = F)
}

#Put all functions together into one complete function
fin <- function(g){
  g <- filt(x=site)
  name <- g
  all.plots(a=name, x=site)
  means(x=name, y=site)
}

###################
## RUN FUNCTIONS ##
###################

un.sites <- unique(sites$SITE_ID_2015)

site <- i
for (i in seq_along(un.sites)){
  fin(g=i)
}

Any help would be so greatly appreciated! Thank you sooooo much!

You pass g but don't use it. You overwrite g, and where does site that you pass to filt as x come from,?

I did, though, didn't I? I passed i through g.

site <- i
for (i in seq_along(un.sites)){
  fin(g=i)
}

I don't know that that's the correct way to do it, though. (Well, obviously not, since it didn't work).

You might pass I as g when you call fin, but when fin is executed , what happens to that value, the code I quoted shows that you immediately give g some other value that's what g <- does

But it works when I manually enter it. If I were to say:

site = "GLNS5-2508"
fin(g=site)

Then that gives me my desired results. What I want to do is have the loop enter the site names for me. Does that make sense?

My understanding is that I'm saying, "hey, g is a subset of data, give that data a name, and then use that name in the plots and means function." But maybe I'm saying something else?

Thanks again for your help with this.

Try removing the seq_along and just loop (i in un.sites)

I did try that unfortunately :frowning: got the same problem of it just looping the same site ID over and over.

That works by accident. As site is the name you call some otherwise undefined variable within your fin function.

This might fix it.

I think you're mixing up your function arguments. In some of your functions,x is a data frame, in other cases x is the site. I fixed by being careful about sending the right arguments. I recommend using descriptive variable names.


filt <- function(x) {
  # x: site
  filter(wq, SITE_ID == x) %>%
    filter(!is.na(DO)) %>%
    filter(!is.na(TEMPERATURE)) %>%
    filter(!is.na(PH)) %>%
    filter(!is.na(CONDUCTIVITY))
}

all.plots <- function(a,x){
  # a: data frame
  # x: site
  p1 <- ggplot() + geom_line(data=a, aes(x=DEPTH, y=DO, color=CAST)) + labs(title = "Dissolved Oxygen (mg/L)", subtitle = x) +
    coord_cartesian(xlim=NULL, ylim=c(0,15))
  p2 <- ggplot() + geom_line(data=a, aes(x=DEPTH, y=PH, color=CAST)) + labs(title = "pH", subtitle = x) +
    coord_cartesian(xlim=NULL, ylim=c(0,14))
  p3 <- ggplot() + geom_line(data=a, aes(x=DEPTH, y=TEMPERATURE, color=CAST)) + labs(title = "Temperature (C)", subtitle = x) +
    coord_cartesian(xlim=NULL, ylim=c(0,30))
  p4 <- ggplot() + geom_line(data=a, aes(x=DEPTH, y=CONDUCTIVITY, color=CAST)) + labs(title = "Conductivity (uS)", subtitle = x) +
    coord_cartesian(xlim=NULL, ylim=c(200,1200))
  #grid.arrange(p1, p2, p3, p4, ncol=2)
}

means <- function(x,y){
  # x: data frame
  # y: site
  site.id <- y
  max.depth <- max(x$DEPTH, na.rm=T)
  mean.do <- mean(x$DO, na.rm=T)
  mean.ph <- mean(x$PH, na.rm=T)
  mean.cond <- mean(x$CONDUCTIVITY, na.rm=T)
  mean.temp <- mean(x$TEMPERATURE, na.rm=T)
  result <- cbind(site.id, max.depth, mean.do, mean.ph, mean.cond, mean.temp)
  print(result)
  write.table(result, file = "practice.ERIE_WQ.csv", append=T, col.names = F)
}

#Put all functions together into one complete function
fin <- function(site){
  g <- filt(site)
  all.plots(g, site)
  means(g, site)
}


###################
## RUN FUNCTIONS ##
###################

un.sites <- unique(sites$SITE_ID_2015)

for (site in un.sites){
  fin(site)
}
1 Like

You are AMAZING! This absolutely worked and yes, clearly using letter variables is a bad idea :joy: Thank you SO MUCH, you saved me so much work of manually entering the values. Thank you, thank you, thank you!

1 Like

Actually, this didn't quite work. What it did was cycle through the different site names but it kep repeating the same results. I changed the final function to look like this:

fin <- function(g=site){
  g <- filt(x=site)
  all.plots(a=g, x=site)
  means(a=g, x=site)
}

And that worked! I think I just didn't know how to write the for loop, and I also didn't need to set g to a different name, that was a redundant step that overrided g. Thanks so much for your help with this, I learned a ton.

The function you give as a solution does not make sense as written. You are assigning the contents of the variable site to g in the function signature, then you immediately replace g with filt(x=site).

When you define a function, using something like function(g = 3) is how you would set a default value for g. Using a variable to set a default like g = site looks really strange to me.

First, about using a default value, suppose we create a function like this:

myfunc <- function(g = 3) {
  g + 4
}

The g = 3 gives a default value, so that when we call the function if we don't pass a value for g it uses the default:

> myfunc()
[1] 7

But if we do give a value it uses that:

> myfunc(1)
[1] 5

If we immediately re-assign g within the function body, it doesn't matter what we pass to the function:

myfunc <- function(g = 3) {
  g <- 25
  g + 4
}
> myfunc()
[1] 29
> myfunc(1)
[1] 29
> 

Note that you don't need to set a default value, and in fact most of the time you probably wouldn't. (If you are writing a package for others to use, then perhaps you would, but in code you are writing for yourself to use in some analysis IMO it is far less likely that you would set default values, and would instead always explicitly include the values when calling the function.)

myfunc <- function(g) {
  g + 4
}

Of course now you must pass a value for g, or the function doesn't work:

> myfunc()
Error in myfunc() : argument "g" is missing, with no default
> myfunc(1)
[1] 5

Now about using a variable to define the defaults, this is particularly unusual! I've never even seen this, so I had to try it out:

def_value <- 11
myfunc <- function(g = def_value) {
  g + 4
}

> myfunc()
[1] 15
> myfunc(1)
[1] 5

def_value <- 4

> myfunc()
[1] 8
> myfunc(1)
[1] 5
> 

Until I actually ran that myfunc() the second time, I wasn't sure if it was going to return 15 or 8! To be honest I thought it would return 15.

I hope that is helpful, I don't mean to be critical but that function just didn't look right.

This topic was automatically closed 7 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.