[英]replicate observation of column for each row in another columns in r
My question may be misleading but is simpler with an example:我的问题可能具有误导性,但举个例子更简单:
imagine I have a dataframe of countries想象一下我有一个国家的 dataframe
Departure
US
FRANCE
BRESIL
I want something like this:我想要这样的东西:
Departure Arrival
US US
FRANCE US
BRESIL US
US FRANCE
FRANCE FRANCE
BRESIL FRANCE
US BRESIL
FRANCE BRESIL
BRESIL BRESIL
But I have actually way more destinations.但我实际上有更多的目的地。 I think there is a simple way to do it but I can't figure it out.
我认为有一种简单的方法可以做到这一点,但我无法弄清楚。 Thanks!
谢谢!
You want something like你想要类似的东西
expand.grid(Departure = Departure, Arrival = Departure)
with your data:使用您的数据:
Departure <- c("US", "FR", "BR")
expand.grid(
Departure = Departure,
Arrival = Departure
)
#> Departure Arrival
#> 1 US US
#> 2 FR US
#> 3 BR US
#> 4 US FR
#> 5 FR FR
#> 6 BR FR
#> 7 US BR
#> 8 FR BR
#> 9 BR BR
Created on 2021-03-25 by the reprex package (v1.0.0)由代表 package (v1.0.0) 于 2021 年 3 月 25 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.