 # Storing iterative results in a loop

Hello House

I have a time series data set for time(m=1-3hrs) of two variables (uwnd and vwnd). For each iteration in the loop, I want the result to be stored in each column. nrow=95200, ncol =1, time =3.

Below please find my script as its not giving the desired results :

result <- data.frame( ) # initialize vector to store results, length = ncol(mydata)

for (m in 1:3) # for(m in 1:ncol(dname1)).
{
uwnd_10.slice <- dname1[, , m]
vwnd_10.slice <- dname2[, , m]

wsp_slice_10 <- matrix(nrow = nlon, ncol = nlat)
for(i in 1:dim(uwnd_10.slice)) {
for(j in 1:dim(vwnd_10.slice)) {
wsp_slice_10[i,j] <- (((uwnd_10.slice[i])^2) + ((vwnd_10.slice[j])^2))^0.5

}
}

lonlat <- expand.grid(lon, lat)
wsp_slice_10.vec <- as.vector(wsp_slice_10)

wsp_slice_10.df01 <- data.frame(cbind(lonlat, wsp_slice_10.vec))

length(wsp_slice_10.vec)
print(wsp_slice_10.vec)

answ <- (wsp_slice_10.df01) # change to assignment

wsp_answ <- answ
result <- rbind(wsp_answ)

output =result
}
length(result)

Hello,

I have a climate data set "dname" comprising of two variables (uwnd and vwnd) for time period of 1-3hours. The dataset is on a regular long and lat points (Nx= 280 , Ny= 340). I need to calculate the wind speed values for the three time periods (iterations, m=3) using the below equation:

`wsp = (((uwnd)^2) + ((vwnd)^2))^0.5`

Also, I want the results to be stored in matrix format "Nx*Ny by time (95200 by 3)". Please note that dname 1 and dname2 are the selected uwnd and vwnd data from dname.nc for time period of 3hours, respectively.

Below please find my written code for review

``````wsp_answ <- matrix(nrow = (nlon* nlat), ncol = 3)

wsp_answ <- data.frame(wsp_answ)

for (m in 1:3)
{
uwnd_10.slice <- dname1[, , m]
vwnd_10.slice <- dname2[, , m]

for(i in 1:dim(uwnd_10.slice))  {
for(j in 1:dim(vwnd_10.slice))  {
wsp_slice_10[i,j] <- (((uwnd_10.slice[i])^2) + ((vwnd_10.slice[j])^2))^0.5

}
}

lonlat <- expand.grid(lon, lat)
wsp_slice_10.vec <- as.vector(wsp_slice_10)

wsp_slice_10.df01 <- data.frame(cbind(lonlat, wsp_slice_10.vec))

answ <- (wsp_slice_10.df01) # change to assignment

wsp_answ <- answ[]

}
``````

The results for the last iteration m=3 is only produced on a matrix of 95200 by 1 instead of m =1,2 and 3 on a matrix of 95200*3 . Useful suggestions would be appreciated.
Thank you.

Sampled datasets for three iterations (m=1:3), nlon = 11 , nlat = 3 .

time = m = 1
Date lon. lat. uwnd. vwnd
2008/12/31 -67.25. -55.75 8.2. -2.6
2009/01/01. -69.75. -55.75 -7.9. 1.2
2009/01/02. -69.25. -55.75 1.4. 4.9
2009/01/03 -68.75. -55.75 -7.8 2.3
2009/01/04 -68.25. -55.25 8.9 -1.2
2009/01/05. -67.75. -55.25. -9.1. 0.8
2009/01/06. -67.25. -55.25 3.0. 1.3
2009/01/07 -71.75. -54.75 -1.8 -1.6
2009/01/08. -70.75. -54.75 8.7. 0.2
2009/01/09. -70.25. -54.75. -3.9. 1.4
2009/01/10. -69.75. -54.75 -2.8. 3.4

time = m = 2
Date lon lat. uwnd. vwnd
2008/12/31 -67.25. -55.75. 2.2. 6.7
2009/01/01. -69.75. -55.75. 9.1. -0.6
2009/01/02. -69.25. -55.75 5.9. 1.3
2009/01/03 -68.75. -55.75 8.8. -2.0
2009/01/04 -68.2. -55.25 3.9. 1.8
2009/01/05. -67.75. -55.25 1.4. -4.2
2009/01/06. -67.25. -55.25 4.2. 1.6
2009/01/07 -71.75. -54.75. 6.8. -2.4
2009/01/08. -70.75. -54.75. 9.8. -0.6
2009/01/09. -70.25. -54.75 3.9. 2.6
2009/01/10. -69.75. -54.75 5.2. -1.8

time = m = 3
Date lon lat. uwnd. vwnd
2008/12/31 -67.25. -55.75 -2.2. 3.4
2009/01/01. -69.75. -55.75 1.9. 6.7
2009/01/02. -69.25. -55.75. -5.4. 2.9
2009/01/03 -68.75. -55.75 3.8. -4.1
2009/01/04 -68.25. -55.25 6.9. -1.4
2009/01/05. -67.75. -55.25. -2.1. 6.0
2009/01/06. -67.25. -55.25 -2.5. 4.8
2009/01/07 -71.75. -54.75. 3.8. -2.6
2009/01/08. -70.75. -54.75 4.7. -1.3
2009/01/09. -70.25. -54.75 -2.5. 4.2
2009/01/10. -69.75. -54.75 -1.8. 5.1

results should look similar to the following format

``````  Date.       wsp_m1.   wsp_m2.    wsp_m3
``````

2008/12/31 2.9. 3.4. 5.4
2009/01/01 3.9. 6.7. 4.8
2009/01/02. 3.4. 2.9. 6.1
2009/01/03 4.8. 5.1. 3.9
2009/01/04 9.4. 4.2. 6.2
2009/01/05. 12.1. 6.0. 8.3
2009/01/06. 5.2. 7.4. 3.7
2009/01/07. 3.2. 5.6. 4.1
2009/01/08. 2.5. 4 .3. 3.8
2009/01/09. 2.9. 3.7. 3.4
2009/01/10. 3.4. 4..1. 6.2

``````wsp_answ <- matrix(nrow = (nlon* nlat), ncol = m=3)

for (m in 1:3)
{
uwnd_10.slice <- uwnd[, , m]
vwnd_10.slice <- vwnd[, , m]

for(i in 1:dim(uwnd_10.slice))  {
for(j in 1:dim(vwnd_10.slice))  {
wsp_slice_10[i,j] <- (((uwnd_10.slice[i])^2) + ((vwnd_10.slice[j])^2))^0.5

}
}

lonlat <- expand.grid(lon, lat)
wsp_slice_10.vec <- as.vector(wsp_slice_10)

wsp_slice_10.df01 <- data.frame(cbind(lonlat, wsp_slice_10.vec))

answ <- (wsp_slice_10.df01) # change to assignment

wsp_answ <- answ[]

}

``````

The code are found below

Hi! Sorry for late reply, but I didn't have any free time the last 2-3 days.

Maybe you're looking for something like this?

``````data_at_time_1 <- read.table(text = 'Date lon lat uwnd vwnd
2008/12/31 -67.25 -55.75 8.2 -2.6
2009/01/01 -69.75 -55.75 -7.9 1.2
2009/01/02 -69.25 -55.75 1.4 4.9
2009/01/03 -68.75 -55.75 -7.8 2.3
2009/01/04 -68.25 -55.25 8.9 -1.2
2009/01/05 -67.75 -55.25 -9.1 0.8
2009/01/06 -67.25 -55.25 3.0 1.3
2009/01/07 -71.75 -54.75 -1.8 -1.6
2009/01/08 -70.75 -54.75 8.7 0.2
2009/01/09 -70.25 -54.75 -3.9 1.4
2009/01/10 -69.75 -54.75 -2.8 3.4',

data_at_time_2 <- read.table(text = 'Date lon lat uwnd vwnd
2008/12/31 -67.25 -55.75 2.2 6.7
2009/01/01 -69.75 -55.75 9.1 -0.6
2009/01/02 -69.25 -55.75 5.9 1.3
2009/01/03 -68.75 -55.75 8.8 -2.0
2009/01/04 -68.25 -55.25 3.9 1.8
2009/01/05 -67.75 -55.25 1.4 -4.2
2009/01/06 -67.25 -55.25 4.2 1.6
2009/01/07 -71.75 -54.75 6.8 -2.4
2009/01/08 -70.75 -54.75 9.8 -0.6
2009/01/09 -70.25 -54.75 3.9 2.6
2009/01/10 -69.75 -54.75 5.2 -1.8',

data_at_time_3 <- read.table(text = 'Date lon lat uwnd vwnd
2008/12/31 -67.25 -55.75 -2.2 3.4
2009/01/01 -69.75 -55.75 1.9 6.7
2009/01/02 -69.25 -55.75 -5.4 2.9
2009/01/03 -68.75 -55.75 3.8 -4.1
2009/01/04 -68.25 -55.25 6.9 -1.4
2009/01/05 -67.75 -55.25 -2.1 6.0
2009/01/06 -67.25 -55.25 -2.5 4.8
2009/01/07 -71.75 -54.75 3.8 -2.6
2009/01/08 -70.75 -54.75 4.7 -1.3
2009/01/09 -70.25 -54.75 -2.5 4.2
2009/01/10 -69.75 -54.75 -1.8 5.1',

Dates = c("2008/12/31", "2009/01/01", "2009/01/02", "2009/01/03", "2009/01/04", "2009/01/05", "2009/01/06", "2009/01/07", "2009/01/08", "2009/01/09", "2009/01/10")

for (m in 1:3)
{
assign(x = paste0("wsp_", m),
value = with(data = eval(expr = parse(text = paste0("data_at_time_", m))),
expr = sqrt(x = ((uwnd ^ 2) + (vwnd ^ 2)))))
}

wsp_results <- data.frame(Dates, mget(x = ls(pattern = "wsp_(\\d)")))

wsp_results
#>         Dates    wsp_1    wsp_2    wsp_3
#> 1  2008/12/31 8.602325 7.051950 4.049691
#> 2  2009/01/01 7.990620 9.119759 6.964194
#> 3  2009/01/02 5.096077 6.041523 6.129437
#> 4  2009/01/03 8.132035 9.024411 5.590170
#> 5  2009/01/04 8.980535 4.295346 7.040597
#> 6  2009/01/05 9.135097 4.427189 6.356886
#> 7  2009/01/06 3.269557 4.494441 5.412024
#> 8  2009/01/07 2.408319 7.211103 4.604346
#> 9  2009/01/08 8.702299 9.818350 4.876474
#> 10 2009/01/09 4.143670 4.687217 4.887740
#> 11 2009/01/10 4.404543 5.502727 5.408327
``````

Created on 2019-07-21 by the reprex package (v0.3.0)

1 Like

Hello,
Thank you so much for this great help and your generated result. The desired format (wsp_1, wsp_2 and wsp_3) as produced by you was the exact results I have trying to achieved.

Please could you send me your email address so that I can send you a sampled dataset in netcdf format. I want to know how you extract/recall the dataset for a number of three iterations (m in 1:3).

Thank you

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.