
## checking rosette_number column


## inital exploration of column
table(mydata$rosette_ID)

## rosette number does not matter much as a column, as in the general ppn dataset there is no consistency
## in tracking rosettes within a plant from year to year
## the only thing we need is for the number of rosette numbers to match the 
## numbers from the column "no_rosettes"


## Survival script edits have created instance where survival = "yes" 
## but there no data for the rosette (all NAs). this is because survival
## was measured at rosette level and not plant level as required in PLANTPOPNET. 
## These rows should be removed now. 

mydata <- mydata[!(mydata$survival == "yes" & is.na(mydata$no_leaves) & is.na(mydata$leaf_length)),]
#155 rows removed as of 19/02/2020 - this may change as data is added. 

## Creating warnings for instances where the number of unique rosette IDs per unique plant
## is not the same as the value for no_rosettes

## speedup using dplyr

dat <- mydata %>%
  group_by(c_year, plant_unique_id) %>%
  mutate(check_rosette_ID2 = no_rosettes / n_distinct(rosette_ID))


## create warnings for and print plant ids where number of unique rosette ids 
## are not the same as the values for no_rosettes per unique plant id

if(any(is.na(dat$check_rosette_ID2))) warning("NA's for")
cat(paste(length(dat$check_rosette_ID2[is.na(dat$check_rosette_ID2)]), "lines of data"))

if(any(dat$check_rosette_ID2 != 1)) warning("incorrect rosette ID's for")
cat(paste(dat$plant_unique_id[!is.na(dat$check_rosette_ID2) & dat$check_rosette_ID2 != 1], 
          max.levels = 0))


    
## if(any(mydata$check_rosette_ID != 1)) warning("incorrect rosettes IDs for")
## print(mydata$plant_unique_id[mydata$check_rosette_ID != 1], max.levels = 0)    
## this line prints an NA if the plant is recorded as dead 



## clean up
mydata <- droplevels(mydata)
rm(check_rosette_ID)

## FINISHED
