gpt4 book ai didi

r - 计算潮差

转载 作者:行者123 更新时间:2023-12-01 12:43:41 26 4
gpt4 key购买 nike

我有一个包含以下潮汐信息的数据框。我正在尝试编写一个带有四个参数的函数(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")

到目前为止,我为高/低部分所做的尝试与时间无关:

  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

}

最佳答案

子集数据是 R 中的一个非常基本的操作,并且有很好的描述,例如在 R 手册中 An Introduction to R .

假设您的数据名为x,使用子集运算符[ 指定要保留的行:

x[x$Pred.Ft < 2, ]

Date Day Time Pred.Ft. Pred.cm. High_Low
4 2013-01-02 Wed 12:03 AM 0.5 15 L
8 2013-01-03 Thu 12:42 AM 1.4 43 L

或者仅在涨潮时:

x[x$Pred.Ft > 6, ]

Date Day Time Pred.Ft. Pred.cm. High_Low
1 2013-01-01 Tue 7:03 AM 8.1 247 H
5 2013-01-02 Wed 7:33 AM 8.1 247 H
9 2013-01-03 Thu 8:03 AM 8.1 247 H

要组合逻辑语句,请使用| 表示OR& 表示AND。因此,要一步获得一组低潮和高潮:

x[x$Pred.Ft > 6 | x$Pred.Ft < 2, ]


Date Day Time Pred.Ft. Pred.cm. High_Low
1 2013-01-01 Tue 7:03 AM 8.1 247 H
4 2013-01-02 Wed 12:03 AM 0.5 15 L
5 2013-01-02 Wed 7:33 AM 8.1 247 H
8 2013-01-03 Thu 12:42 AM 1.4 43 L
9 2013-01-03 Thu 8:03 AM 8.1 247 H

要仅在大潮中获得高潮,请尝试此操作。因为您知道每个低点后面都有一个高点,所以您可以使用 diff 计算潮位差,然后只返回差值高于阈值的行:

x$Tidediff <- c(NA, diff(x$Pred.Ft))
na.omit(x[x$Tidediff > 6, ])

Date Day Time Pred.Ft. Pred.cm. High_Low Tidediff
5 2013-01-02 Wed 7:33 AM 8.1 247 H 7.6
9 2013-01-03 Thu 8:03 AM 8.1 247 H 6.7

关于r - 计算潮差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14544762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com