简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM