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.
--
View this message in context:
http://r.789695.n4.nabble.com/copy-the-columns-based-on-the-code-tp4505253p4505253.html
Sent from the R help mailing list archive at Nabble.com.
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
Thanks for the reply.
I am using the function you gave me.
complete.travel<-merge(travel, city, by.x = "Source", by.y =
"cod", all TRUE)
complete.travel<-merge(travel, city, by.x = "Destine", by.y =
"cod", all TRUE
The problem is that it gives the result that I want
The idea is based on the column of source and intended Identify the
cities and put a new data structure
The idea is something like this.
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
--
View this message in context:
http://r.789695.n4.nabble.com/copy-the-columns-based-on-the-code-tp4505253p4505939.html
Sent from the R help mailing list archive at Nabble.com.