Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:For loop使用谐波成分执行潮汐分析_R_Loops_For Loop_Apply - Fatal编程技术网

R:For loop使用谐波成分执行潮汐分析

R:For loop使用谐波成分执行潮汐分析,r,loops,for-loop,apply,R,Loops,For Loop,Apply,**经过编辑,我取得了进步,但我认为我的原始问题并没有得到应有的完善 一般来说,我对R和计算机编程是新手,我正在尝试编写我的第一个for循环 我想用NOAA的谐波成分做一些潮汐分析 我的初始数据=如下所示的数据: Constituent # Name Amplitude Phase Speed 1 M2 3.264 29.0 28.98 2 S2

**经过编辑,我取得了进步,但我认为我的原始问题并没有得到应有的完善

一般来说,我对R和计算机编程是新手,我正在尝试编写我的第一个for循环

我想用NOAA的谐波成分做一些潮汐分析

我的初始数据=如下所示的数据:

Constituent #   Name    Amplitude      Phase      Speed        
1                M2      3.264         29.0       28.98 
2                S2      0.781         51.9       30.0  
3                N2      0.63          12.3       28.43 
4                K1      1.263        136.8       15.04 
5                M4      0.043        286.0       57.96 
波高方程为
h(t)=振幅*cos(速度*t相位)
,其中t为时间

因此,我需要对每个成分(行)执行此计算,并按时间对每个成分的结果求和

因此,我的中间结果将是一个表,其中ncols=时间戳的数量,nrow=成分的数量

                   T1                               T2                              T3...
data[1,3]*cos(data[1,4]*T1-data[1,3])    data[1,3]*cos(data[1,4]*T2-data[1,3])
data[2,3]*cos(data[2,4]*T1-data[2,3])    data[2,3]*cos(data[2,4]*T2-data[2,3])
                   .
                   .
                   .

data[n,3]*cos(data[n,4]*T1-data[n,3])    data[n,3]*cos(data[n,4]*T2-data[n,3])
有了这个表格,我可以对各列求和,得到每个时间戳的潮汐高度的最终答案

为此,我尝试创建一个for循环

   DF=NULL

for (i in 1:nrow(data)){
  DF<- matrix(c(DF, data[i,2]*cos(pi/180*(data[i,4]*Time[,]-data[i,3]))))

}
DF=NULL
用于(i/1:nrow(数据)){

DF也许这不是一个答案-但我会建议一种不同的方法。我将使用R中的包
data.table

library(data.table)

#use own location of your data
wave_table=fread(input="F:\\wave.csv");

wave_table
#           Constituent Name Amplitude Phase Speed
#    1:           1   M2     3.264  29.0 28.98
#    2:           2   S2     0.781  51.9 30.00
#    3:           3   N2     0.630  12.3 28.43
#    4:           4   K1     1.263 136.8 15.04
#    5:           5   M4     0.043 286.0 57.96

#create a function which does your calculation on the named columns of your data, 
#taking time 't' as a parameter

hh<-function(t){ wave_table[,{Amplitude*cos(Speed*t-Phase)}] }
hh2<-function(t) wave_table[,{Amplitude*cos(Speed*t-Phase)}, by=Name]
hh3<-function(t) wave_table[,{Amplitude*cos(Speed*t-Phase)}, by=Constituent]
hh4<-function(t) wave_table[,{sum(Amplitude*cos(Speed*t-Phase))}, by=Constituent]

#Now the function `hh` can be used like this, giving you a bit 
#more flexibility with what you want to do, perhaps?

hh(1)
#3.26334722 -0.77775795 -0.57472163 -0.91362687 -0.01165717

一般来说,对于这类问题,应该避免使用R中的循环,因为它们很慢/有更好的工具可用。循环通常是“最后的手段”


如果函数
hh
hh4
不能完全满足您的要求,则可以使用其他变体。请查看

我使用了Rusan的方法,并完成了我在上面问题中添加的操作。这很有效,但如果有更好的方法,请发表意见。仅对我指定的函数发表意见
hh3
。这可能会也可能不会做你认为它在做的事情,我建议你检查一个小的手动案例。特别是,当你传递一个向量/矩阵时,你需要检查data.table和function方法是否在做你想让它做的事情。注意,我只为标量
t
演示了它至少对(其他人)有帮助如果你展示一个你期望的输出的例子,那么你对这个公式到底做了什么。例如,你是对成分或名称求和,还是两者都不求和?thanksI做了一个检查,这就是我需要的。它比for循环简单得多,初学者也更容易理解。在你的实际数据中,你有多个常量吗iTunes?或多个名称..或两者都有?我正在尝试确定您计算的数量(高度)是按名称(M2、K1等)还是按成分(1、2等)报告的或者两者都没有:在您的计算末尾只有一个高度值?在您的玩具数据中,成分列和名称列都包含唯一的项目。我可能误解了,成分可能只是“行号”而不是数据的一部分,等等。确切地说,多个成分的名称是行号,然后是成分名称。我相信它们是回归分析的一部分,所以每个成分在波高中都起着作用。例如,M2是主要的月球半日变化成分,我需要在一个月内将所有成分相加计算特定时间的海浪高度需要一定的时间。我刚刚开始为一个工作项目学习这些知识,但是如果你有兴趣,你可以在NOAA网站上找到更多。这不是推荐的方法,但类似于这项工作:
as.data.table(hh3(1:100))[,{sum(V1)},by=Name][,{sum(V1)}]
?其中
hh3
hh3
library(data.table)

#use own location of your data
wave_table=fread(input="F:\\wave.csv");

wave_table
#           Constituent Name Amplitude Phase Speed
#    1:           1   M2     3.264  29.0 28.98
#    2:           2   S2     0.781  51.9 30.00
#    3:           3   N2     0.630  12.3 28.43
#    4:           4   K1     1.263 136.8 15.04
#    5:           5   M4     0.043 286.0 57.96

#create a function which does your calculation on the named columns of your data, 
#taking time 't' as a parameter

hh<-function(t){ wave_table[,{Amplitude*cos(Speed*t-Phase)}] }
hh2<-function(t) wave_table[,{Amplitude*cos(Speed*t-Phase)}, by=Name]
hh3<-function(t) wave_table[,{Amplitude*cos(Speed*t-Phase)}, by=Constituent]
hh4<-function(t) wave_table[,{sum(Amplitude*cos(Speed*t-Phase))}, by=Constituent]

#Now the function `hh` can be used like this, giving you a bit 
#more flexibility with what you want to do, perhaps?

hh(1)
#3.26334722 -0.77775795 -0.57472163 -0.91362687 -0.01165717
hh2(1)
#   Name          V1
#1:   M2  3.26334722
#2:   S2 -0.77775795
#3:   N2 -0.57472163
#4:   K1 -0.91362687
#5:   M4 -0.01165717
hh4(1) #after adding an extra row to your data: "Constituent=1, Name=M3, 
#Amp=1.263,Phase=51.9, Speed=15.04
#   Constituent          V1
#1:           1  4.10718774
#2:           2 -0.77775795
#3:           3 -0.57472163
#4:           4 -0.91362687
#5:           5 -0.01165717