BiologicalRecordsCentre / sparta

Species Presence/Absence R Trends Analyses
http://biologicalrecordscentre.github.io/sparta/index.html
MIT License
21 stars 24 forks source link

frescalo date problem #66

Closed EichenbergBEF closed 6 years ago

EichenbergBEF commented 6 years ago

Hi there. I am trying to use the frescalo function and I prepared the data according to the frescalo example in the vignette. However, instead of the unicorn example I use my own data. When I'm trying to run the frescalo function, it however throws an error

`frescalo(Data = myData, frespath = myFrescaloPath, time_periods = myTimePeriods, site_col = 'site', sp_col = 'species', year_col = 'year', Fres_weights = myWeights, sinkdir = myFolder, phi=0.89, alpha=0.27)

Error in frescalo_checks(site_col, sp_col, year_col, start_col, end_col, : _column specified by yearcol must be numeric`

Here's a tibble from my data, clearly stating that the year_col is of type numeric:

A tibble: 9,400 x 4 species year site year_1 chr dbl int dbl 1 Barbarea verna 1986. 19382 1986. 2 Barbarea verna 1993. 19412 1993. 3 Barbarea verna 1993. 19422 1993. 4 Barbarea verna 1984. 20414 1984. 5 Barbarea verna 1984. 20423 1984. 6 Barbarea verna 1984. 20424 1984. 7 Barbarea verna 1988. 21443 1988. 8 Barbarea verna 1993. 22402 1993. 9 Barbarea verna 1984. 23431 1984. 10 Barbarea verna 1987. 20314 1987. ... with 9,390 more rows

I also tried to foramt the year column as a Date using as.Date() as well as by using as.POSIXlt() and as.POSIXct(). This did not solve the problem.

Any idea, what I am missing?

AugustT commented 6 years ago

Can you try this for me

is.numeric(myData['year'][,1])
EichenbergBEF commented 6 years ago

Hey Tom, thank you very much for your help! Oh, that's interesting! is.numeric(myData['year'][,1]) [1] FALSE

while

is.numeric(myData$year) [1] TRUE

Can you explain what is the difference between the two? Great new insight! And how do I get R to accept myData['year'][,1] as numeric?

AugustT commented 6 years ago

Can you show me

str(myData)
str(myData$year)
str(myData['year'][,1])

I'm not sure why I used this catch rather than is.numeric(myData$year) ... myData['year'][,1] seems a strange formulation, but I wonder if I had a good reason for it?

EichenbergBEF commented 6 years ago

Haha, cool!

Okay, let's see:

str(myData) Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 9400 obs. of 4 variables: $ species: chr "Barbarea verna" "Barbarea verna" "Barbarea verna" "Barbarea verna" ... $ year : num 1986 1993 1993 1984 1984 ... $ site : int 19382 19412 19422 20414 20423 20424 21443 22402 23431 20314 ... $ year_1 : num 1986 1993 1993 1984 1984 ...

str(myData$year) num [1:9400] 1986 1993 1993 1984 1984 ...

str(myData['year'][,1]) Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 9400 obs. of 1 variable: $ year: num 1986 1993 1993 1984 1984 ...

All numeric... strange.

FYI: the 'new' column 'year_1' is identical to 'year', I just createdit to experiment with different variable modes.

EichenbergBEF commented 6 years ago

BTW, if I try this: as.numeric(myData['year'][,1])

I get an error saying: Error: (list) object cannot be coerced to type 'double'

I don't know wether it's relevant, but maybe this gives a hint.

EichenbergBEF commented 6 years ago

Okay, I have found something: the problem seemed to be that the data ist stored as a 'tibble'. When I converted it explicitly to a dataframe, it works:

myData_1<- as.data.frame(myData)

So there seems to be a problem with as_tibble.

Thank you very much for your assistance and time. From my side this problem is solved. Cheers, David

AugustT commented 6 years ago

Well worked out!

this gave it away

str(myData['year'][,1])
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   9400 obs. of 1 variable:
$ year: num 1986 1993 1993 1984 1984 ...

This should return a single number. It is a shame tibbles don't work so this might be worth me changing anyway. Thanks fr reporting the issue.