Pierce, Ken
2006-Sep-20 22:42 UTC
[R] problem coercing truncated character vector to levels
Dear R wizes, I have a data.frame of species abundances with column names consisting of 4 letter codes then an underscore and a number like this: abco_1, abco_2, abco_3, psm_1, psme_2, psme_3, etc. I would like to get an identifier for all the abco, and psme and other species etc. I used spec.fact<-substring(names(spec.count),1,4) To make a vector of the first 4 letters of each name. I tried using spec.groups <- unique(spec.fact) and then matching pmatch(spec.groups[1],spec.fact) to get out the indexes of all the columns that match the second unique value, i.e what are the column number for all the abco columns. Then I could make a temporary data.frame of just the abco columns. I could loop through the unique vector spec.groups to do this for each group and perform the operations I need to on each individual mini-dataframe. Unfortunately the matching doesn't work. Any suggestions? Ken Kenneth B. Pierce Jr. Research Ecologist Landscape Ecology, Modeling, Mapping and Analysis Team PNW Research Station - USDA-FS 3200 SW Jefferson Way, Corvallis, OR 97331 ken.pierce@oregonstate.edu 541 750-7393 http://www.fsl.orst.edu/lemma/gnnfire [[alternative HTML version deleted]]
Pierce, Ken
2006-Sep-20 23:07 UTC
[R] problem coercing truncated character vector to levels
Thanks. The problem is I have an variable number of these indices. I have figured part of it out and it involves a code like this with the indented column being the important one. Of course I work on this for 4 hours and figure part of it out about 2 minutes after posting. The answer involves subsetting directly on a matched level value. spec.fact<-substring(names(spec.count),1,4) spec.col <- data.frame(ni(spec.count),spec.fact) spec.fact2 <- factor(spec.fact) spec.fact2 i<-7 spec.sub<- spec.col[spec.col[,3]==levels(spec.fact2)[i],] spec.wk <- frcheck[,spec.sub[,2]] dim(spec.wk) Ken ________________________________ From: nalluri pratap [mailto:pratap_stat@yahoo.co.in] Sent: Wednesday, September 20, 2006 4:03 PM To: Pierce, Ken Subject: Re: [R] problem coercing truncated character vector to levels hi, Try this one spec.count<- strtrim(c("abco_1", "abco_2", "abco_3", "psm_1", "psme_2", "psme_3"),c(4,4,4,3,4,4)) spec.groups <- unique(spec.count) pmatch(spec.groups[1],spec.fact) Thanks and Regards, Pratap "Pierce, Ken" <ken.pierce@oregonstate.edu> wrote: Dear R wizes, I have a data.frame of species abundances with column names consisting of 4 letter codes then an underscore and a number like this: abco_1, abco_2, abco_3, psm_1, psme_2, psme_3, etc. I would like to get an identifier for all the abco, and psme and other species etc. I used spec.fact<-substring(names(spec.count),1,4) To make a vector of the first 4 letters of each name. I tried using spec.groups <- unique(spec.fact) and then matching pmatch(spec.groups[1],spec.fact) to get out the indexes of all the columns that match the second unique value, i.e what are the column number for all the abco columns. Then I could make a temporary data.frame of just the abco columns. I could loop through the unique vector spec.groups to do this for each group and perform the operations I need to on each individual mini-dataframe. Unfortunately the matching doesn't work. Any suggestions? Ken Kenneth B. Pierce Jr. Research Ecologist Landscape Ecology, Modeling, Mapping and Analysis Team PNW Research Station - USDA-FS 3200 SW Jefferson Way, Corvallis, OR 97331 ken.pierce@oregonstate.edu 541 750-7393 http://www.fsl.orst.edu/lemma/gnnfire [[alternative HTML version deleted]] ______________________________________________ R-help@stat.math.ethz.ch 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. ________________________________ Find out what India is talking about on - Yahoo! Answers India <http://us.rd.yahoo.com/mail/in/yanswers/*http://in.answers.yahoo.com/> Send FREE SMS to your friend's mobile from Yahoo! Messenger Version 8. Get it NOW <http://us.rd.yahoo.com/mail/in/messengertagline/*http://in.messenger.ya hoo.com> [[alternative HTML version deleted]]