[R] new column from column in another df
jim holtman
jholtman at gmail.com
Tue Nov 9 14:00:47 CET 2010
?merge
> df1
V1 V2
1 Populus tremula 1
2 Populus tremula 2
3 Populus tremula 3
4 Calluna vulgaris 1
5 Calluna vulgaris 2
6 Betula alba 1
7 Betula alba 2
8 Betula alba 3
9 Primula veris 1
10 Primula veris 2
> df2 <- read.table('clipboard', sep=',')
> df2
V1 V2
1 Populus tremula tree
2 Acer platanoides tree
3 Ribes rubrum shrub
4 Calluna vulgaris dwarf_shrub
5 Betula alba tree
6 Primula veris herb
> merge(df1, df2, by = "V1")
V1 V2.x V2.y
1 Betula alba 1 tree
2 Betula alba 2 tree
3 Betula alba 3 tree
4 Calluna vulgaris 1 dwarf_shrub
5 Calluna vulgaris 2 dwarf_shrub
6 Populus tremula 1 tree
7 Populus tremula 2 tree
8 Populus tremula 3 tree
9 Primula veris 1 herb
10 Primula veris 2 herb
>
On Tue, Nov 9, 2010 at 7:28 AM, fugelpitch <jonas at runtimerecords.net> wrote:
>
> 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.
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list