Error with "select" function when building a hypervolume

Hi!
I have been trying to create a hypervolume using climatic data and species data. The data itself is ready, but I am encountering problems when building the hypervolume, especifically when selecting the climatic variables and single species I want to use. I have properly installed and loaded the Tidyverse package, but I can't seem to be able to get it to work. Here is my code (including all my different attempts and errors):

hv1 <- hypervolume(df.clim.occ_wider %>% filter(., "species" == "Celtis australis") %>% select(mean_temperature, mean_precipitation, seasonality_temperature, seasonilty_precipitation))
#Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘select’ for signature ‘"mts"’
hv1 <- hypervolume(df.clim.occ_wider %>% dplyr::select(., "species" == "Celtis australis") %>% select(mean_temperature, mean_precipitation, seasonality_temperature, seasonilty_precipitation))

error in evaluating the argument 'x' in selecting a method for function 'select': Must subset columns with a valid subscript vector

hv1 <- hypervolume(df.clim.occ_wider %>% select(., "species" == "Celtis australis") %>% select(mean_temperature, mean_precipitation, seasonality_temperature, seasonilty_precipitation))
error in evaluating the argument 'x' in selecting a method for function 'select': unable to find an inherited method for function ‘select’ for signature ‘"tbl_df"’

I have looked for especific information about the hzpervolume package but have not found anzthing helpful.

This likely means either

  1. A namespace conflict with base::select. Test by explicitly using dplyr::select
  2. do.clim.occ_wider is not a data frame. Test with class()
  3. Both 1 & 2

Thank you for your quick answer. I have checked with class() and it is indeed a data frame. I have tried using dplyr::select exclusively in the command line and get this error:

hv1 <- hypervolume(df.clim.occ_wider %>% dplyr::select(., "species" == "Celtis australis") %>% dplyr::select(mean_temperature, mean_precipitation, seasonality_temperature, seasonilty_precipitation))
Error in dplyr::select():
! Must subset columns with a valid subscript vector.
:heavy_multiplication_x: Subscript has the wrong type logical.
:information_source: It must be numeric or character.
Run rlang::last_error() to see where the error occurred.
rlang::last_error()
<error/vctrs_error_subscript_type>
Error in dplyr::select():
! Must subset columns with a valid subscript vector.
:heavy_multiplication_x: Subscript has the wrong type logical.
:information_source: It must be numeric or character.


Backtrace:

  1. hypervolume::hypervolume(...)
  2. rlang::cnd_signal(x)
    Run rlang::last_trace() to see the full context.

Not sure what it means by this since the dataframe seems to be fine.

this is causing your error.
What is your intent by this statement ?

1 Like

I wanted to select only the data from that especific plant species. I was trying to create one hypervolume per species, so first I had to select the species from my dataframe.

Rows or columns ? I assume you mean only the rows for the species. Use dplyr filter for that rather than dplyr select.

Good catch, and explains the error because species == something will return a TRUE or FALSE. One of the things that drives me nuts about dplyr is keeping select and filter straight: select is for columns and filter is for rows, which likes the typeof logical return value.

I see now that I might have mixed up rows and columns. I will check the dataframe again and try it out. Thank you very much.

It seems you were both right. Now I am not getting that error, but a new one:

hv1 <- hypervolume(df.clim.occ_wider %>% filter(., "species" == "Celtis australis") %>% select(mean_temperature, mean_precipitation, seasonality_temperature, seasonality_precipitation)) #Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘select’ for signature ‘"mts"’
Error in hypervolume(df.clim.occ_wider %>% filter(., "species" == "Celtis australis") %>% :
Hypervolume cannot be computed with empty input data.

I have checked my data and it is indeed not empty, so I do not know what it means by this.

My advice is that you split out your data preparation from running your calculation.

mydata <- df.clim.occ_wider %>% filter(., "species" == "Celtis australis") %>% select(mean_temperature, mean_precipitation, seasonality_temperature, seasonality_precipitation)

hv1 <- hypervolume(mydata)

Most likely the filter is not quite matching the data and nothing is returned. Things like capitalisation and whitespace might explain, among other possibilities

Not sure if it plays a role, but the period in .,select is unnecessary here.

Understood. I will try that out. Thank you.

I see. I will delete it as well. Thanks.

I made the changes suggested, and still get the same error:

dataCeltisaustralis <- df.clim.occ_wider %>% filter(., "species" == "Celtis australis") %>% select(mean_temperature, mean_precipitation, seasonality_temperature, seasonality_precipitation)

hypervolume(dataCeltisaustralis)
Error in hypervolume(dataCeltisaustralis) :
Hypervolume cannot be computed with empty input data.

Curiouslz, deleting the dot in .,select caused another error, so I left it there just in case. I have checked the species name and all other variables and they are properly written, with all variables containing data in the table, so I am not sure where this empty input data could be comming from. Any other suggestions?

What does just displaying dataCeltisaustralis show?

Ok, I have just checked and apparently ther eis no data in the table. 0 observations of 4 variables, so the data is not properly loaded. Any idea on how this could be fixed?

Do everything one step at a time and see where it is in the process that something turns into nothing. Without knowing more, I nominate the filter.

Try removing the quote marks

Yes! That was It! Now the displayed data is shown properly and the hypervolume is obtained with no issue! Thank you very much for your help! :handshake:

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.