[R] copy the columns based on the code

Sarah Goslee sarah.goslee at gmail.com
Mon Mar 26 15:38:28 CEST 2012


It isn't quite clear to me that cod in data frame city and pos in
travel are are actually the same index, but if so then you can easily
use merge() for this task.

Sarah

On Mon, Mar 26, 2012 at 5:41 AM, MSousa <ricardosousa2000 at clix.pt> wrote:
>
>  Hello,
>
> I have two different dataset, and wanted to join the two.
>   For example I have a table of codes of  cities, and other with with the
> codes of travels, [source for the destine].
> what he wanted was to have a new data.frame with all the information
>
> city<-data.frame(city="Barcelona",cod=1)
> city<-rbind(city,data.frame(city="Madrid",cod=2))
> city<-rbind(city,data.frame(city="Lisbon",cod=3)))
> city<-rbind(city,data.frame(city="Milan",cod=4))
> city<-rbind(city,data.frame(city="London",cod=5))
>
> travel<-data.frame(pos=1,Source=1,Destine=2)
> travel<-rbind(travel,data.frame(pos=1,Source=1,Destine=3))
> travel<-rbind(travel,data.frame(pos=2,Source=3,Destine=4))
> travel<-rbind(travel,data.frame(pos=3,Source=2,Destine=4))
> travel<-rbind(travel,data.frame(pos=4,Source=1,Destine=3))
>
> #for example
> pos Source     city       Destine  city_destine
> 1      1       Barcelona   2       Madrid
> 1      1       Barcelona   3       Lisbon
> 2      3       Lisbon      4       Milan
> 3      2       Madrid      4       Milan
>
> which the fastest way to do this.
>
>    Thanks.
>
>


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list