R中的for循环仅返回最后一次迭代

时间:2015-07-30 04:24:04

标签: r

我是R的新手,也是编程的新手。所以我的问题可能很愚蠢,但任何帮助都会受到赞赏。

包含输入的csv文件如下所示:

Width   Depth   WL1 WL2 V1   V2
0       0       50  52  0    0
0       95      50  52  0.4  0.41
100     100     50  52  0.5  0.5
200     130     50  52  0.7  0.73
300     125     50  52  0.46 0.48
400     115     50  52  0.25 0.28
500     95      50  52  0.24 0.24
600     75      50  52  0.4  0.41
700     80      50  52  0.32 0.3
800     80      50  52  0.09 0.1
900     70      50  52  0    0
1000    60      50  52  0   0
1100    50      50  52  0   0
1200    40      50  52  0   0
1300    30      50  52  0   0
1400    30      50  52  0   0
1500    15      50  52  0   0
1600    5       50  52      
1600    0       50  52      

所以我正在读取.csv文件中的输入来进行河流流量计算(total_flow)。水位和速度的值随时间变化,我已经存储在csv文件的连续列中。代码正在精确计算最后一次迭代的放电。如何更改我的代码以获得所有迭代的放电?我的代码如下所示:

field_data <- read.csv("Information_VRH-11-09-14.csv", header = TRUE)
# field_data
width <- field_data[[1]]
width
# length(width)
actual_depth <- field_data[[2]]
actual_depth
# length(actual_depth)
for (i in 3:4)
{
    water_level <- field_data[[i]]
}

# Calculations for depth data
ref_depth <- actual_depth[2:18]
ref_depth
water_level <- water_level[2:18]
water_level
depth_data <- ref_depth - water_level
depth_data
for (i in 1:length(depth_data))
{
    if (depth_data[i] < 0) 
        depth_data[i] <- 0
}
depth_data
dL <- depth_data[1:16]
dR <- depth_data[2:17]

# Submerged width of the trapezoids
xdata <- width[2:18]
xdata
length(xdata)
n <- length(xdata)
xL <- xdata[1:16]
xR <- xdata[2:17]

# Velocity
for (i in 5:6)
{
    velocity <- field_data[[i]]
    vdata <- velocity[2:(length(xdata))]
}
vdata

# Calculation of discharge and area of trapezoids
Area <- (((xR - xL) * (dR + dL))/2)/10000
Area
flow <- Area * vdata
flow
total_flow <- sum(flow)
total_flow 

0 个答案:

没有答案
相关问题