Unused Argument Error in trapz ()

When I calculate the area under curve using trapz () of pracma package, it always showed the following error:

Error in pracma::trapz(., wlPGP, L30) : unused argument (L30)

data.frame(time = c("2015-01-08 10:30:00", "2015-01-08 12:30:00", 
    "2015-01-08 13:00:00", "2015-01-08 13:30:00", "2015-01-08 14:00:00", 
    "2015-01-08 10:30:00", "2015-01-08 12:30:00", "2015-01-08 13:00:00", 
    "2015-01-08 13:30:00", "2015-01-08 14:00:00", "2015-01-08 10:30:00", 
    "2015-01-08 12:30:00", "2015-01-08 13:00:00", "2015-01-08 13:30:00", 
    "2015-01-08 14:00:00"), wlPGP = c(750.33, 750.33, 750.33, 
    750.33, 750.33, 750.74, 750.74, 750.74, 750.74, 750.74, 751.15, 
    751.15, 751.15, 751.15, 751.15), L30 = c(34.8381682164527, 
    16.4037816745955, 16.1198492561195, 13.7315701800825, 13.6046706387906, 
    34.5523259386597, 16.2183131142457, 15.8960774061323, 13.5871635950505, 
    13.4227034871889, 34.4351879005441, 16.075073665463, 15.8311421893508, 
    13.4960208643065, 13.4213241443487))
#>                   time  wlPGP      L30
#> 1  2015-01-08 10:30:00 750.33 34.83817
#> 2  2015-01-08 12:30:00 750.33 16.40378
#> 3  2015-01-08 13:00:00 750.33 16.11985
#> 4  2015-01-08 13:30:00 750.33 13.73157
#> 5  2015-01-08 14:00:00 750.33 13.60467
#> 6  2015-01-08 10:30:00 750.74 34.55233
#> 7  2015-01-08 12:30:00 750.74 16.21831
#> 8  2015-01-08 13:00:00 750.74 15.89608
#> 9  2015-01-08 13:30:00 750.74 13.58716
#> 10 2015-01-08 14:00:00 750.74 13.42270
#> 11 2015-01-08 10:30:00 751.15 34.43519
#> 12 2015-01-08 12:30:00 751.15 16.07507
#> 13 2015-01-08 13:00:00 751.15 15.83114
#> 14 2015-01-08 13:30:00 751.15 13.49602
#> 15 2015-01-08 14:00:00 751.15 13.42132

Using the above dataset, I write a code as follow:

A <- data.frame(........)
Area_A <- A %>%
filter(wlPGP > 750 & wlPGP < 752) %>%
group_by(time) %>%
pracma::trapz(wlPGP, L30)

When I run the code, it shows:
Error in pracma::trapz(., wlPGP, L30) : unused argument (L30)

How could I solve it?

Thanks in advance~

Here is an instruction on how to do the reprex:

Is it workable? @mishabalyasin @Yarnabrina

no, it's not.

Please, read the link I've posted and try again. It should be possible for me to copy-paste your code verbatim into RStudio and it'll run without an issue.

I can't see the dataset in viewer after dpasta().

I hope it's okay now @mishabalyasin

data.frame(time = c("2015-01-08 10:30:00", "2015-01-08 12:30:00", 
    "2015-01-08 13:00:00", "2015-01-08 13:30:00", "2015-01-08 14:00:00", 
    "2015-01-08 10:30:00", "2015-01-08 12:30:00", "2015-01-08 13:00:00", 
    "2015-01-08 13:30:00", "2015-01-08 14:00:00", "2015-01-08 10:30:00", 
    "2015-01-08 12:30:00", "2015-01-08 13:00:00", "2015-01-08 13:30:00", 
    "2015-01-08 14:00:00"), wlPGP = c(750.33, 750.33, 750.33, 
    750.33, 750.33, 750.74, 750.74, 750.74, 750.74, 750.74, 751.15, 
    751.15, 751.15, 751.15, 751.15), L30 = c(34.8381682164527, 
    16.4037816745955, 16.1198492561195, 13.7315701800825, 13.6046706387906, 
    34.5523259386597, 16.2183131142457, 15.8960774061323, 13.5871635950505, 
    13.4227034871889, 34.4351879005441, 16.075073665463, 15.8311421893508, 
    13.4960208643065, 13.4213241443487))
#>                   time  wlPGP      L30
#> 1  2015-01-08 10:30:00 750.33 34.83817
#> 2  2015-01-08 12:30:00 750.33 16.40378
#> 3  2015-01-08 13:00:00 750.33 16.11985
#> 4  2015-01-08 13:30:00 750.33 13.73157
#> 5  2015-01-08 14:00:00 750.33 13.60467
#> 6  2015-01-08 10:30:00 750.74 34.55233
#> 7  2015-01-08 12:30:00 750.74 16.21831
#> 8  2015-01-08 13:00:00 750.74 15.89608
#> 9  2015-01-08 13:30:00 750.74 13.58716
#> 10 2015-01-08 14:00:00 750.74 13.42270
#> 11 2015-01-08 10:30:00 751.15 34.43519
#> 12 2015-01-08 12:30:00 751.15 16.07507
#> 13 2015-01-08 13:00:00 751.15 15.83114
#> 14 2015-01-08 13:30:00 751.15 13.49602
#> 15 2015-01-08 14:00:00 751.15 13.42132

Using the above dataset, I write a code as follow:
A <- data.frame(........)
Area_A <- A %>%
filter(wlPGP > 750 & wlPGP < 752) %>%
group_by(time) %>%
pracma::trapz(wlPGP, L30)

When I run the code, it shows:
Error in pracma::trapz(., wlPGP, L30) : unused argument (L30)

You are almost there, but your previous message finally contains the dataframe.
From the documentation for the trapz function I see that it takes two vectors (x and y, but can be anything).

What I assume you are trying to do is something like this (notice how it's a proper reprex):

library(tidyverse)
A <- data.frame(time = c("2015-01-08 10:30:00", "2015-01-08 12:30:00", 
                         "2015-01-08 13:00:00", "2015-01-08 13:30:00", "2015-01-08 14:00:00", 
                         "2015-01-08 10:30:00", "2015-01-08 12:30:00", "2015-01-08 13:00:00", 
                         "2015-01-08 13:30:00", "2015-01-08 14:00:00", "2015-01-08 10:30:00", 
                         "2015-01-08 12:30:00", "2015-01-08 13:00:00", "2015-01-08 13:30:00", 
                         "2015-01-08 14:00:00"), wlPGP = c(750.33, 750.33, 750.33, 
                                                           750.33, 750.33, 750.74, 750.74, 750.74, 750.74, 750.74, 751.15, 
                                                           751.15, 751.15, 751.15, 751.15), L30 = c(34.8381682164527, 
                                                                                                    16.4037816745955, 16.1198492561195, 13.7315701800825, 13.6046706387906, 
                                                                                                    34.5523259386597, 16.2183131142457, 15.8960774061323, 13.5871635950505, 
                                                                                                    13.4227034871889, 34.4351879005441, 16.075073665463, 15.8311421893508, 
                                                                                                    13.4960208643065, 13.4213241443487))

Area_A <- A %>%
  dplyr::filter(wlPGP > 750 & wlPGP < 752) %>%
  dplyr::group_by(time) %>%
  dplyr::summarize(result = pracma::trapz(wlPGP, L30))
Area_A
#> # A tibble: 5 × 2
#>   time                result
#>   <chr>                <dbl>
#> 1 2015-01-08 10:30:00   28.4
#> 2 2015-01-08 12:30:00   13.3
#> 3 2015-01-08 13:00:00   13.1
#> 4 2015-01-08 13:30:00   11.2
#> 5 2015-01-08 14:00:00   11.0

Created on 2021-09-08 by the reprex package (v2.0.1)

Does that do what you want?

2 Likes

Thank you @mishabalyasin ! It works! Could you please help me a bit more? I want to calculate B1 and B2. Now, according to your kind help, I could calculate B2. First, I thought B1 can be calculated by deducing B2 from 1.
It seems not to be like this, based on the result. How could I code for B1 as well?
I tried to code as follow:

Left_edge_L30 <- A %>%
  filter(wlPGP>750 & wlPGP <775)%>%
  select(wlPGP, time, L30) %>%
  group_by(time, wlPGP)%>%
  dplyr::summarise(sum_L30 = sum(L30)) %>%
  group_by(time) %>%
  slice_max(sum_L30) %>%
  plyr::rename(c("wlPGP" = "Left_edge_L"))

Right_edge_L30 <- A %>%
  filter(wlPGP>800 & wlPGP <850)%>%
  select(wlPGP, time, L30) %>%
  group_by(time, wlPGP)%>%
  dplyr::summarise(sum_L30 = sum(L30)) %>%
  group_by(time) %>%
  slice_max(sum_L30)%>%
  plyr::rename(c("wlPGP" = "Right_edge_L"))

Edge_com <- A %>%
  left_join(Left_edge_L30, by = c("time")) %>%
  left_join(Right_edge_L30, by = c("time")) %>%
  select(wlPGP, time, L30, Left_edge_L, Right_edge_L)

B_total <- Edge_com %>%
  filter(wlPGP > 750 & wlPGP < 849) %>%
  arrange(time, wlPGP) %>%
  mutate(L30_Left_edge = if_else(wlPGP == Left_edge_L, L30, NA_real_)) %>%
  group_by(time) %>%
  fill(L30_Left_edge,.direction = "down") %>%
  fill(L30_Left_edge,.direction = "up") %>%
  mutate(L30_Right_edge = if_else(wlPGP == Right_edge_L, L30, NA_real_)) %>%
  group_by(time) %>%
  fill(L30_Right_edge,.direction = "down") %>%
  fill(L30_Right_edge,.direction = "up") %>%
  mutate(dis_wl_L = Right_edge_L - Left_edge_L) %>%
  mutate(B_total = (E30_Left_edge + E30_Right_edge)*0.5*dis_wl_E) %>%
  group_by(time) %>%
  dplyr::summarize(across(ends_with("total"), mean, .names = "{.col}"))

When I coded as above, the results of B total is less than B2. Actually, B1 should be B total - B2. In my understanding. Could you please help me?
Thanks in advance!
image

This goes way too in-depth and beyond your initial question.

In order to answer it, I'll need to spend a lot of time understanding your domain which, arguably, is not required. Especially, since here we are trying to help you with R problems, not so much with debugging your understanding about the domain (this you should probably ask in your research group/Google) .

So, please, think about your problem and try to reduce it to the smallest possible unit so that people here don't need to understand what "Solar irradiance" is, for example.

And once you do have a small example of the problem you are having, it is best to ask a new question (don't forget about a reproducible example).

1 Like

Oh sorry, @mishabalyasin ! I didn't mean to ask you the deep understanding of my field. I just asked the area calculation using trapz() function. It seems I made a mistake in coding. That's why, I just confirmed you.
Thank you so much for your kind help :slight_smile:

Perhaps something like this.

The mistake you made at first is incorrect use of the pipe.

This code assumes the max L30 for each time is what defines the dotted line. If this is not the case, we require a column in the data that defines the dotted line.

library(tidyverse)

df <- data.frame(
  time = c("2015-01-08 10:30:00", "2015-01-08 12:30:00", 
           "2015-01-08 13:00:00", "2015-01-08 13:30:00", "2015-01-08 14:00:00", 
           "2015-01-08 10:30:00", "2015-01-08 12:30:00", "2015-01-08 13:00:00", 
           "2015-01-08 13:30:00", "2015-01-08 14:00:00", "2015-01-08 10:30:00", 
           "2015-01-08 12:30:00", "2015-01-08 13:00:00", "2015-01-08 13:30:00", 
           "2015-01-08 14:00:00"), 
  wlPGP = c(750.33, 750.33, 750.33, 
            750.33, 750.33, 750.74, 750.74, 750.74, 750.74, 750.74, 751.15, 
            751.15, 751.15, 751.15, 751.15), 
  L30 = c(34.8381682164527, 
          16.4037816745955, 16.1198492561195, 13.7315701800825, 13.6046706387906, 
          34.5523259386597, 16.2183131142457, 15.8960774061323, 13.5871635950505, 
          13.4227034871889, 34.4351879005441, 16.075073665463, 15.8311421893508, 
          13.4960208643065, 13.4213241443487)
)

# should plot first to see what you're integrating
df %>%
  ggplot() + 
  aes(wlPGP, L30) + 
  geom_line() + 
  facet_wrap(~time, scales = "free")


df %>%
  filter(wlPGP > 750 & wlPGP < 752) %>%
  group_by(time) %>%
  mutate(max_L30 = max(L30)) %>%   # assume the max L30 for each time is what defines the
                                   # dotted line.  if this is not the case, require a column 
                                   # in the data that defines the dotted line
  summarize(
    B  = pracma::trapz(wlPGP, max_L30),  # area under dottted line
    B2 = pracma::trapz(wlPGP, L30),      # area under solid line
    B1 = B - B2                          # difference
  )
#> # A tibble: 5 x 4
#>   time                    B    B2    B1
#> * <chr>               <dbl> <dbl> <dbl>
#> 1 2015-01-08 10:30:00  28.6  28.4 0.200
#> 2 2015-01-08 12:30:00  13.5  13.3 0.143
#> 3 2015-01-08 13:00:00  13.2  13.1 0.151
#> 4 2015-01-08 13:30:00  11.3  11.2 0.107
#> 5 2015-01-08 14:00:00  11.2  11.0 0.112

Created on 2021-09-10 by the reprex package (v1.0.0)

1 Like

Thank you, @arthur.t ! Your code works very well. It meets my need! Thank you very much again!

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.