Please help me with R!
Posted: Tue Feb 16, 2021 12:13 pm
I shall preface by saying I'm not very good with R or coding in general. I can usually just about get by because I'm not doing anything hugely complicated. This doesn't feel like a hugely complicated task but my googling has come to naught. I'm hoping someone is going to take one look at this, roll their eyes at how simple the solution is, and help me out.
My dataset comprises trace element analyses for a load of fish otoliths from a load of different sites. The dataset comprises multiple trace element measurements for each otolith providing a bit of a time series but for this analysis I'm just interested in the core (birth) and margin (death). I've also got 5 different location categories, with site being the most specific and the others are various management areas.
In the past I created separate spreadsheets for the core and margin data and another separate spreadsheet with the site means, but I'm trying to be better and work off a single spreadsheet, so need to create these datasets internally in R.
I've been able to create the core and margin datasets using subset easily enough but it's calculating the means where I'm having difficulty. I tried using melt which retained the location categories but when I cast to calculate the means I hit a snag - all the location categories except site (as I'm interested in the site means) are discarded. I need a way of calculating site means but retaining the other location category columns.
Is melt and cast the best way to do this or is there another way I'm missing?
My melt/cast code is this:
My dataset comprises trace element analyses for a load of fish otoliths from a load of different sites. The dataset comprises multiple trace element measurements for each otolith providing a bit of a time series but for this analysis I'm just interested in the core (birth) and margin (death). I've also got 5 different location categories, with site being the most specific and the others are various management areas.
In the past I created separate spreadsheets for the core and margin data and another separate spreadsheet with the site means, but I'm trying to be better and work off a single spreadsheet, so need to create these datasets internally in R.
I've been able to create the core and margin datasets using subset easily enough but it's calculating the means where I'm having difficulty. I tried using melt which retained the location categories but when I cast to calculate the means I hit a snag - all the location categories except site (as I'm interested in the site means) are discarded. I need a way of calculating site means but retaining the other location category columns.
Is melt and cast the best way to do this or is there another way I'm missing?
My melt/cast code is this:
Code: Select all
LC_core_melt <- melt(LC_core, id=c("Fish_Code", "Fisheries", "MEOW_Province", "MEOW_Ecoregion", "IMCRA_Bioregion", "Site"))
LC_site_means <- cast(LC_core_melt, Site~variable, mean)