[R] Odp: count of factors
Petr PIKAL
petr.pikal at precheza.cz
Wed Jan 19 10:58:06 CET 2011
Hi
r-help-bounces at r-project.org napsal dne 19.01.2011 09:51:43:
>
> Hello everyone,
>
> I have a data set like this:
>
> > head( fish_transect)
> ID_TRANSECT ID_PROJECT DE_ZONE DE_LOCALITY DE_SECTOR MES
> 1 42 MB Tarragona Creixell Control I 9
> 2 42 MB Tarragona Creixell Control I 9
> 3 42 MB Tarragona Creixell Control I 9
> 4 42 MB Tarragona Creixell Control I 9
> 5 42 MB Tarragona Creixell Control I 9
> 6 42 MB Tarragona Creixell Control I 9
> ID_SPECIES WEIGHT SIZE N FAMILIA
> 1 Spondyliosoma cantharus 15.64 10 1 Sparidae
> 2 Symphodus melanocercus 11.21 10 1 Labridae
> 3 Diplodus vulgaris 30.20 10 2 Sparidae
> 4 Diplodus vulgaris 52.24 12 2 Sparidae
> 5 Diplodus sargus 221.41 14 5 Sparidae
> 6 Diplodus annularis 3.47 6 1 Sparidae
>
> The table function of the column FAMILIA gives:
<snip>
>
> I want to add a new column "familia2" where those families with low
number
> of individuals are listed as "other", that is, for these families
> Atherinidae Blenniidae Bothidae Carangidae
Centracanthidae
> 1 2 11 25
> 4
> Congridae Gadidae Haemulidae Mugilidae
> Muraenidae
> 21 2 25 10
> 45
> Myliobatidae Scombridae Sphyraenidae Synodontidae
> Torpedinidae
> 2 1 5 1
> 1
> Trachinidae
> 1
>
Use levels
make new column
fish_transect$familia2 <- fish_transect$familia
change levels
levels(fish_transect$familia2)[which(table(fish_transect$familia2)<46)]<-"other"
You can use any threshold number.
Regards
Petr
BTW you need to have familia as factor not as character variable.
> I want them in column "familia2" to be named "others"
>
> I've been trying to do a loop telling R to write "others" when the sum
of
> FAMILIA was less than 50, but it didn't work because FAMILIA is a
factor,
> and there isn't a "count" function (or I didn't find it). I also tried
other
> options like the "match" function, "level" function, etc. I know I can
do it
> manually typing by myself the names of the 16 families but I want to
learn a
> way to do where I don't need to type the names.
>
> Thanks in advance!
>
> Lucia
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/count-of-factors-
> tp3224791p3224791.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list