0

I'm sorry if this sounds dumb or isn't clear. I am fairly new to R but trying to take on a big task. I want to simulate wind dispersal over various time frames using the same calculations for different timed data. I have a lot of calculations which need to be done for each 1 hour time slot of a year and I want to write a code to allow me to just write the maths and define the data to come from row 1, then automatically run the same calculations with the data in row 2 without overwriting the outcomes.

I'll need a table of sorts to show me the output of each run. I've been looking in my books, checking the R packages and I'm just getting a bit lost. I just want to make something which will do something similar to density = mass / volume (row 1), (row 2), row (3) etc...

Not looking for exact codes, just more a push in the right direction. At the moment I'm consider manually doing it, which is possible but I have to do 1 hourly time steps for a whole year, it's a lot.

Thanks in advance.

So my data is: weather data (over 8000 lines) And the type of calculations I want to do are: physics calculations

  • 1
    Could you post an example of your input data? and a stub of what the output should look like? – dalloliogm Aug 01 '22 at 13:55
  • At the moment I'm going through them one-by-one, i.e. then running line 2 etc but there are so many! p.sat_1 <- 6.1078 * 10**((7.5 * mydata$air_temperature[1])/(mydata$air_temperature [1] + 237.3)) water_vapour_pressure_1 <- p.sat_1 * mydata$humidity_0.1.0 [1] – Dounia Lamtali Aug 01 '22 at 15:15
  • Please post some of your data (see https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), one example of the calculation, and the desired results. You can easily solve this problem without going one-by-one, but the answer cannot be answered in it's current form. The core idea would be to put the data into a data frame and add a new column with the calculations based on all the other rows. – harre Aug 01 '22 at 17:18
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 01 '22 at 22:54

2 Answers2

0

A reproducible data frame would allow me to give a better answer, but a really simple example would be something like this

library(tidyverse)

df <- tibble(
  time = c("00:00","00:15","00:30","00:45","01:00"),
  x = c(1,2,3,4,5),
  y = c(6,7,8,9,0),
  z = c(3,6,9,12,15)
)

result <- tibble(
  time =  NULL,
  res = NULL
)                                       ## create and empty object to store values

for(i in 1:length(df$time)){
  
  result_func <- tibble(
    time = df$time[i],
    res = df$x[i]+df$y[i]+df$z[i]         ## this would be your weather calculation
  )
  
  result <<- rbind(result,result_func)  ## this adds the result to your empty object
  
  rm(result_func)
  
}

I would recommend looking here for more complex tasks: https://purrr.tidyverse.org/reference/map.html

Hope this is useful.

W4tRb0y
  • 99
  • 3
  • There is really no need to use a loop or `rbind` results, as it's all vectorized: `df$res <- df$x + df$y + df$z` yields the same results in one line of code. And when you've loaded `tidyverse` why not use `mutate(res = x + y + z)`? – harre Aug 01 '22 at 17:21
  • Yep agree that would work for my example dataframe, however, I don't think the user wanted the columns adding to the original data, but rather a new output table. As Im not sure how wide the users dataframe is this made more sense for me, as each formula they may use may only use specific columns, and the overall dataframe would be quite cumbersome. oh yeah good spot, I just mean to write tibble instead of tidyverse! – W4tRb0y Aug 01 '22 at 17:52
0

Thanks everyone for all your help and suggestions, I have managed to make it work using the function code:

p.sat <- function(x) {
x <- mydata$air_temperature
x <- 6.1078 * 10**((7.5 * mydata$air_temperature)/(mydata$air_temperature + 237.3))
x}

I've then been using these outputs to run the next steps in the formula's:

water_vapour_pressure <- function(x) {
x <- mydata$humidity_0.1.0
x <- sapply(1, p.sat) * mydata$humidity_0.1.0
x}

Seems to be working well. I will then make a separate data frame with all of my output's etc.

:)