我有一个包含以下潮汐信息的数据框。我正在尝试编写一个需要四个参数(low.max、hi.max、hi.earliest、hi.latest)的函数。例如,向我展示所有低点为 2 英尺或更低、嗨点为 6 英尺或更低、嗨点发生在上午 10 点到下午 4 点之间的所有日子。现在我正在遍历行来执行此操作(有点让 hi.max - low.max 使用它),但我是 R 新手,并假设有一种更像 R 的方法。
date day time ft cm H/L
2013/01/01 Tue 07:03 AM 8.1 247 H
2013/01/01 Tue 12:49 PM 5.1 155 L
2013/01/01 Tue 05:30 PM 5.7 174 H
2013/01/02 Wed 12:03 AM 0.5 15 L
2013/01/02 Wed 07:33 AM 8.1 247 H
2013/01/02 Wed 01:40 PM 4.4 134 L
2013/01/02 Wed 06:32 PM 5.3 162 H
2013/01/03 Thu 12:42 AM 1.4 43 L
2013/01/03 Thu 08:03 AM 8.1 247 H
2013/01/03 Thu 02:33 PM 3.5 107 L
2013/01/03 Thu 07:46 PM 4.9 149 H
添加 dput 输出:
structure(list(Date = structure(c(15706, 15706, 15706, 15707,
15707, 15707, 15707, 15708, 15708, 15708), class = "Date"), Day = c("Tue",
"Tue", "Tue", "Wed", "Wed", "Wed", "Wed", "Thu", "Thu", "Thu"
), Time = c("7:03 AM", "12:49 PM", "5:30 PM", "12:03 AM", "7:33 AM",
"1:40 PM", "6:32 PM", "12:42 AM", "8:03 AM", "2:33 PM"), Pred.Ft. = c(8.1,
5.1, 5.7, 0.5, 8.1, 4.4, 5.3, 1.4, 8.1, 3.5), Pred.cm. = c(247L,
155L, 174L, 15L, 247L, 134L, 162L, 43L, 247L, 107L), High_Low = c("H",
"L", "H", "L", "H", "L", "H", "L", "H", "L")), .Names = c("Date",
"Day", "Time", "Pred.Ft.", "Pred.cm.", "High_Low"), row.names = c(NA,
10L), class = "data.frame")
到目前为止,我为 hi/lo 部分所做的尝试与时间无关:
tides <- read.csv("TideData.csv", stringsAsFactors = FALSE)
for (i in 1: nrow(tides)){
if (tides[i, 6] == "L" & tides[i, 4] <= low.max
& tides[i+1, 6] == "H" & tides[i+1, 4] <= hi.max){
#deal with last iteration being out of bounds / write out to a df
}