G’day All,
I am working in R. Sorry about this really basic question, but I am a bit stuck.
I have a data set of presence/absence point count data with date of count, and site number (see below). I would like to ultimately create a data.frame that collates all counts by grid cell number and has each visit to a site as a new visit (see below). I can’t figure out how to do this, so thought I would take an easier route and make a column that gives a visit number for each record. So, the column would give a number for each record by the date of the visit within each site group (see below). I can’t figure out how to do this either!
Any help would be great, thank you in advance.
Kind regards,
Adam
I have this:
Site date
1 12/01/2000
1 24/02/2000
1 13/08/2001
2 14/01/2000
2 21/01/2002
3 1/01/1999
3 21/04/2000
Ultimately want this:
Site vist1 v2 v3
1 12/01/2000 24/02/2000 13/08/2001
2 14/01/2000 21/01/2002 na
3 01/01/1999 21/04/2000 na
But this would be good:
Site date visit
1 12/01/2000 1
1 24/02/2000 2
1 13/08/2001 3
2 14/01/2000 1
2 21/01/2002 2
3 01/01/1999 1
3 21/04/2000 2
Basically, you are wanting to reshape your data from a long format to a wide format, with repeated observations from a
Siteall in a single line. The base R functionreshape()was designed for just this task.The only (slight) complication is that you first need to add a column (which I here call
obsNum) that identifies which is the first, second, third etc. observation at aSite. By settingtimevar = "obsNum", you can then letreshape()know into which column you want to put each of the values ofdate.