[R] Odp: Sorting data from one column with strings

Petr PIKAL
Thu, 04 Nov 2010 13:26:01 -0000

Hi

r-help-bounces at r-project.org napsal dne 04.11.2010 13:28:06:

>
> Hello,
>
> I have tried to find this out some other way, but unsuccessful I have to
> try this list.
> I assume this should be quite simple.
>
> I have a dataset with 4 columns, "Sample_no",
"Species", "Nitrogen",
> "Carbon" in csv format. In the species column I have many
different
> species with varying number of obs per species
>
> Eg
>
> "Sample_no"   "Species"   "Nitrogen"
"Carbon"
> 1      Cod      15.2      -19.0
> 2      Haddock   14.8      -20.2
> 3      Cod      15.6      -18.5
> 4      Cod      13.2      -20.1
> 5      Haddock   14.3      -18.8
> Etc..
>
> And I want to calculate, mean, standard dev etc per species for the
> observations "Nitrogen" and "Carbon". And later do
plots and stats with
> the different species. I will in the end have many species, so need it
to
> be "automatic" I can't enter code for every species separate.

No need for sorting. You can us R. Particularly ?tapply, ?by or ?aggregate
commands. Regarding plots you can consider lattice or ggplot2, but you can
get good results also with base graphics.

aggregate(your.data[,3:4], list(yourdata$Species), function(x) c(mean(x),
sd(x)))
xyplot(nitrogen~carbon|species, data=your.data)

Regards
Petr