Stephen,
You probably should name your dataframe 'dat' and
replace the line
x <- subset(x, Creek=="fbms" & station==i)
with
x <- subset(dat, Creek=="fbms" & station==i)
-Peter Ehlers
stephen sefick wrote:> Both of the approx functions work correctly individually, but they are
> not being distinguished in the for loop by the if statments. Any help
> would be appreciated. for loop of interest is below
>
> x <- (structure(list(Site = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label >
c("Big_Muddy_Creek_Main_Stem",
> "Field_Branch_Main_Stem", "Flat_Creek_Main_Stem",
"Jennie_Creek_Main_Stem",
> "Juniper_Creek_Main_Stem",
"Little_Rockfish_Main_Stem_North",
> "Wolf_Pit_Creek_Main_Stem"), class = "factor"), Base =
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, 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 = "ftbr", class = "factor"), Creek =
structure(c(2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L), .Label = c("bmcm", "fbms", "fcms",
"jcms", "jpms", "lrf1",
> "wpms"), class = "factor"), Date = structure(c(2L, 2L,
2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label >
c("2009-09-05",
> "2009-09-06", "2009-09-07", "2009-09-12",
"2009-09-13", "2009-09-14"
> ), class = "factor"), elevation_m = c(77, 77, 77, 77, 77, 77,
> 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
> 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
> 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
> 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
> 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77), x = c(655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L, 655608L, 655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L, 655608L, 655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L, 655608L, 655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L, 655608L, 655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L, 655608L, 655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L, 655608L, 655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L, 655608L, 655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L, 655608L, 655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L, 655608L, 655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L, 655608L, 655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L, 655608L, 655608L,
> 655608L, 655608L, 655608L, 655608L, 655608L), y = c(3881598L,
> 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L,
> 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L,
> 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L,
> 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L,
> 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L,
> 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L,
> 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L,
> 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L,
> 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L,
> 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L,
> 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L, 3881598L,
> 3881598L, 3881598L, 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.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.18, 0.18,
> 0.18), bankfull_depth_left = c(0.08, 0.08, 0.08, 0.08, 0.08,
> 0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08,
> 0.08, 0.08, 0.08, 0.08, 0.08, 0.21, 0.06, 0.06, 0.06, 0.06, 0.06,
> 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
> 0.06, 0.06, 0.06, 0.06, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04,
> 0.04, 0.04, 0.04, 0.04, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09,
> 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09,
> 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.08, 0.08,
> 0.08), bankfull_width_m = c(4.04, 4.04, 4.04, 4.04, 4.04, 4.04,
> 4.04, 4.04, 4.04, 4.04, 4.04, 4.04, 4.04, 4.04, 4.04, 4.04, 4.04,
> 4.04, 4.04, 4.04, 4.04, 3.9, 3.78, 3.78, 3.78, 3.78, 3.78, 3.78,
> 3.78, 3.78, 3.78, 3.78, 3.78, 3.78, 3.78, 3.78, 3.78, 3.78, 3.78,
> 3.78, 3.78, 3.78, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3.23, 3.23,
> 3.23, 3.23, 3.23, 3.23, 3.23, 3.23, 3.23, 3.23, 3.23, 3.23, 3.23,
> 3.23, 3.23, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1, 2.1,
> 2.1, 2.1, 4.04, 4.04, 4.04), wetted_width_m = c(2.24, 2.24, 2.24,
> 2.24, 2.24, 2.24, 2.24, 2.24, 2.24, 2.24, 2.24, 2.24, 2.24, 2.24,
> 2.24, 2.24, 2.24, 2.24, 2.24, 2.24, 2.24, 2.1, 2.35, 2.35, 2.35,
> 2.35, 2.35, 2.35, 2.35, 2.35, 2.35, 2.35, 2.35, 2.35, 2.35, 2.35,
> 2.35, 2.35, 2.35, 2.35, 2.35, 2.35, 1.2, 1.2, 1.2, 1.2, 1.2,
> 1.2, 1.2, 1.2, 1.2, 1.2, 1.2, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3,
> 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 1.6, 1.6, 1.6, 1.6, 1.6,
> 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 2.24, 2.24, 2.24),
> bankfull_correction_m = c(0.18,
> 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18,
> 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.21, 0.06,
> 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
> 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.21, 0.21, 0.21,
> 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.09, 0.09, 0.09,
> 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09,
> 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09,
> 0.09, 0.09, 0.18, 0.18, 0.18), measurment_num = c(1L, 2L, 3L,
> 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
> 18L, 19L, 20L, 21L, 1L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L,
> 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 11L, 10L,
> 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 15L, 14L, 13L, 12L, 11L,
> 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 12L, 11L, 10L, 9L, 8L,
> 7L, 6L, 5L, 4L, 3L, 2L, 1L, 21L, 20L, 19L), bankfull_depths_m = c(0,
> 0.02, 0.1, 0.52, 0.56, 0.61, 0.63, 0.67, 0.68, 0.72, 0.74, 0.73,
> 0.74, 0.7, 0.7, 0.63, 0.22, 0.13, 0.09, 0.02, -0.1, -0.19, 0,
> -0.01, 0.08, 0.24, 0.97, 1, 1.04, 1.11, 1.12, 1.14, 1.14, 1.14,
> 1.09, 1.11, 1.08, 1.04, 0.44, 0.28, 0.07, -0.06, -0.17, -0.13,
> -0.04, 0.76, 0.78, 0.79, 0.83, 0.83, 0.81, 0.09, 0, 0, 0.03,
> 0.21, 0.77, 0.85, 0.87, 0.92, 0.93, 0.91, 0.91, 0.88, 0.89, 0.85,
> 0.06, -0.09, 0, 0.01, 0.7, 0.74, 0.71, 0.72, 0.72, 0.73, 0.68,
> 0.6, 0.03, -0.09, -0.1, 0.02, 0.09), water_surface_m = c(NA,
> NA, NA, NA, 0.05, 0.13, 0.13, 0.18, 0.2, 0.22, 0.22, 0.22, 0.22,
> 0.16, 0.14, 0.05, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.02,
> 0.08, 0.12, 0.18, 0.2, 0.22, 0.21, 0.21, 0.16, 0.15, 0.14, 0.09,
> NA, NA, NA, NA, NA, NA, NA, 0.23, 0.24, 0.24, 0.29, 0.29, 0.27,
> NA, NA, NA, NA, NA, 0.08, 0.18, 0.19, 0.23, 0.24, 0.22, 0.23,
> 0.21, 0.2, 0.17, NA, NA, NA, NA, 0.3, 0.33, 0.32, 0.35, 0.34,
> 0.34, 0.3, 0.2, NA, NA, NA, NA, NA), X = 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,
> 1L, 1L, 1L, 5L, 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("", "changed from 1.2 to 1.02",
"changed from 1.4 to 1.04",
> "changed from 1.6 to 1.06", "marked as no bankfull
correction-I don't
> know if this is correct"
> ), class = "factor"), measurment_num_m = c(0, 0.2, 0.4, 0.6,
> 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3, 3.2, 3.4,
> 3.6, 3.8, 4, 0, 3.8, 3.6, 3.4, 3.2, 3, 2.8, 2.6, 2.4, 2.2, 2,
> 1.8, 1.6, 1.4, 1.2, 1, 0.8, 0.6, 0.4, 0.2, 0, 2, 1.8, 1.6, 1.4,
> 1.2, 1, 0.8, 0.6, 0.4, 0.2, 0, 2.8, 2.6, 2.4, 2.2, 2, 1.8, 1.6,
> 1.4, 1.2, 1, 0.8, 0.6, 0.4, 0.2, 0, 2.2, 2, 1.8, 1.6, 1.4, 1.2,
> 1, 0.8, 0.6, 0.4, 0.2, 0, 4, 3.8, 3.6)), .Names = c("Site",
"Base",
> "Creek", "Date", "elevation_m",
"x", "y", "station",
> "bankfull_measurement_interval_m",
> "bankfull_depth_right", "bankfull_depth_left",
"bankfull_width_m",
> "wetted_width_m", "bankfull_correction_m",
"measurment_num",
> "bankfull_depths_m", "water_surface_m", "X",
"measurment_num_m"
> ), row.names = c("463", "464", "465",
"466", "467", "468", "469",
> "470", "471", "472", "473",
"474", "475", "476", "477",
"478",
> "479", "480", "481", "482",
"483", "542", "541", "540",
"539",
> "538", "537", "536", "535",
"534", "533", "532", "531",
"530",
> "529", "528", "527", "526",
"525", "524", "523", "522",
"521",
> "520", "519", "518", "517",
"516", "515", "514", "513",
"512",
> "511", "510", "509", "508",
"507", "506", "505", "504",
"503",
> "502", "501", "500", "499",
"498", "497", "496", "495",
"494",
> "493", "492", "491", "490",
"489", "488", "487", "486",
"485",
> "484", "483.1", "482.1", "481.1"),
class = "data.frame"))
>
> g <- vector()
> for(i in 1:2){
> x <- subset(x, Creek=="fbms" & station==i)
>
> if(x[1,"bankfull_correction_m"]>0){
> y.x <- approxfun(x=x$bankfull_depths_m[7:length(x$bankfull_depths_m)],
> y=x$measurment_num_m[7:length(x$bankfull_depths_m)])
> g[i] <- y.x(0)
> }else if(x[1,"bankfull_correction_m"]<0){
> y.x <- approxfun(x=x$bankfull_depths_m[1:5], y=x$measurment_num_m[1:5])
> g[i] <- y.x(0)
> }
> }
>
--
Peter Ehlers
University of Calgary