Hi all, Given a data frame as...> head(veg)genus species trophia type geo zone importance 1 Sphagnum subsecundum M A En 100 2 Sphagnum denticulatum M A En 200 3 Molinia caerulea M A En 300 4 Sphagnum flexuosum M A En 400 5 Juncus squarrosus M A En 500 6 Carex echinata M A En 600 I do need a new one with a new factor constructed from a concatenation of the two first columns, genus and species. I am guessing this must be a simple matter while working with R, but I am stuck at this point. Any help will be welcome! Thanks. --- Ricardo Rodr?guez Your XEN ICT Team
> tmp <- data.frame(G=factor(letters[c(1,2,3,1,2,3)]),+ S=factor(LETTERS[c(1,1,1,2,2,2)]))> tmpG S 1 a A 2 b A 3 c A 4 a B 5 b B 6 c B> tmp$G.S <- with(tmp, interaction(G, S)) > tmpG S G.S 1 a A a.A 2 b A b.A 3 c A c.A 4 a B a.B 5 b B b.B 6 c B c.B>
On Sun, 2006-12-31 at 00:25 +0100, Ricardo Rodr?guez wrote:> Hi all, > > Given a data frame as... > > > head(veg) > genus species trophia type geo zone importance > 1 Sphagnum subsecundum M A En 100 > 2 Sphagnum denticulatum M A En 200 > 3 Molinia caerulea M A En 300 > 4 Sphagnum flexuosum M A En 400 > 5 Juncus squarrosus M A En 500 > 6 Carex echinata M A En 600 > > I do need a new one with a new factor constructed from a concatenation of the two first columns, genus and species. I am guessing this must be a simple matter while working with R, but I am stuck at this point.veg$genus.species <- paste(veg$genus,veg$species) will add a new column combining genus and species. -- Manuel A. Morales http://mutualism.williams.edu -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part Url : https://stat.ethz.ch/pipermail/r-help/attachments/20061230/270b8505/attachment.bin
One way I do this is, assuming the two columns are characters (or are transformed using as.character()), concatenate them using paste, and then make the new variable a factor. blah = paste(as.character(genus), as.character(species)) new.factor = as.factor(blah) Ricardo Rodríguez wrote:> Hi all, > > Given a data frame as... > > >> head(veg) >> > genus species trophia type geo zone importance > 1 Sphagnum subsecundum M A En 100 > 2 Sphagnum denticulatum M A En 200 > 3 Molinia caerulea M A En 300 > 4 Sphagnum flexuosum M A En 400 > 5 Juncus squarrosus M A En 500 > 6 Carex echinata M A En 600 > > I do need a new one with a new factor constructed from a concatenation of the two first columns, genus and species. I am guessing this must be a simple matter while working with R, but I am stuck at this point. > > Any help will be welcome! Thanks. > > --- > Ricardo Rodríguez > Your XEN ICT Team > > ______________________________________________ > 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. >[[alternative HTML version deleted]]