[R] new column from column in another df
fugelpitch
jonas at runtimerecords.net
Tue Nov 9 13:28:07 CET 2010
If I have a data frame where a species occupies several rows with different
phases such as (both col's ar factors):
species,phase
Populus tremula,1
Populus tremula,2
Populus tremula,3
Calluna vulgaris,1
Calluna vulgaris,2
Betula alba,1
Betula alba,2
Betula alba,3
Primula veris,1
Primula veris,2
and another df where each species only have one row:
species,growth_form
Populus tremula,tree
Acer platanoides,tree
Ribes rubrum,shrub
Calluna vulgaris,dwarf_shrub
Betula alba,tree
Primula veris,herb
...how can I create a new column in the first data frame where growth form
is picked up from the second data frame (also factors) and entered into all
rows for a species as follows:
species,phase,growth_form
Populus tremula,1,tree
Populus tremula,2,tree
Populus tremula,3,tree
Calluna vulgaris,1,dwarf_shrub
Calluna vulgaris,2,dwarf_shrub
Betula alba,1,tree
Betula alba,2,tree
Betula alba,3,tree
Primula veris,1,herb
Primula veris,2,herb
This will be made for data frames a lot larger than this one so it needs to
be automated in some way.
Also, as you can see the second data frame contains more species than the
first one so I need to pick them out by name not only by row number...
(I tried something like:
subset(dataframe2.df,
dataframe2.df$species==as.character(unique(dataframe1.df$species)))
in a for loop but I got an error about different factor levels which is
true.)
Any help is very appreciated!
Jonas
--
View this message in context: http://r.789695.n4.nabble.com/new-column-from-column-in-another-df-tp3033619p3033619.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list