fishR-Core-Team / FSA

FSA (Fisheries Stock Assessment) package provides R functions to conduct typical introductory fisheries analyses.
https://fishr-core-team.github.io/FSA/
GNU General Public License v2.0
66 stars 22 forks source link

Error: Variable in 'formula' must be numeric. #75

Closed daveglover closed 3 years ago

daveglover commented 3 years ago

I'm having an issue with certain functions that require a "formula" such as psdCalc and psdPlot. Length is not being recognized as numeric. I simplified my data frame to just one species and associated lengths (nearest mm) to demonstrate the issue. I'm able to run the simulated yellow perch data from examples in the help files just fine. Love the FSA package and book, btw.

df <- rawdata[c("Species","Length")] psd <- psdCalc(~Length, data=df, species="Black Crappie", units="mm", digits=1) Error: Variable in 'formula' must be numeric. str(df) tibble [244 x 2] (S3: tbl_df/tbl/data.frame) $ Species: chr [1:244] "Black Crappie" "Black Crappie" "Black Crappie" "Black Crappie" ... $ Length : num [1:244] 178 308 213 223 226 117 209 227 276 266 ... is.numeric(df$Length) [1] TRUE

Thanks in advance for any help! Dave

droglenc commented 3 years ago

Dave,

Thanks for the note.

Is there any possibility that your "Length" variable is actually "Length " (with the extra space on the end). Your results from str() imply that that might be the case (there is a space between "Length" and the colon but not between "Species" and the colon).

If not, I don't see the issue immediately. Is it possible for you to send me your data file so that I can try repeating the issue and then troubleshooting from there. You can send it directly to derek@derekogle.com.

Thanks.

daveglover commented 3 years ago

That didn't seem to be the issue. I did find, however, that if I explicitly define df as a data frame I have no issues. I assumed I was working with a data frame, but it turns out when I import my data from a CSV file the data is coming in as class = tbl_df.

df <- data.frame(rawdata[c("Species", "Length")])

droglenc commented 3 years ago

That was my second guess as I have had problems with other functions relative to the data being a tibble (the tbl_df). I will get that fixed for the next version. I will let you know when it is fixed in the development version so you can give that a try if you would like as it may be a while before we release the next version to CRAN.

Thanks for letting me know and thanks for your patience.

daveglover commented 3 years ago

Easy enough to be more explicit in my programming. Thank you!

droglenc commented 3 years ago

More and more people are using methods to read data that result in tbl_df, so we need to fix this. Re-opening so I remember to fix this.