[R] copy the columns based on the code

jim holtman jholtman at gmail.com
Tue Mar 27 18:30:44 CEST 2012


yet another way:

> 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))
>
> travel$city <- city$city[match(travel$Source, city$cod)]
> travel$city_destine <- city$city[match(travel$Destine, city$cod)]
>
> travel
  pos Source Destine      city city_destine
1   1      1       2 Barcelona       Madrid
2   1      1       3 Barcelona       Lisbon
3   2      3       4    Lisbon        Milan
4   3      2       4    Madrid        Milan
5   4      1       3 Barcelona       Lisbon
>


On Tue, Mar 27, 2012 at 12:15 PM, MSousa <ricardosousa2000 at clix.pt> wrote:
>
>
> Hello,
>
> this code, works perfectly
>   temp <- merge(travel, city, by.x="Source", by.y="cod")
>   result <- merge(temp, city, by.x="Destine", by.y="cod")
>
> The problem was the construction of the data frame, had a parenthesis in
> city<-rbind(city,data.frame(city="Lisbon",cod=3))),
>
> I tried to delete the post, but i don't could.
>  As I have little experience in R, I still do some mistakes.
> I use read.table to load the data frame, the way in the post, it was quickly
> that  i found to describe the problem.
>  The forum has been a great help for me.
>
> Thanks
>
>
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/copy-the-columns-based-on-the-code-tp4505253p4509340.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
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list