wscjnc
wscjnc

Reputation: 9

How to convert a dataframe into a list of coordinates

I have created a data frame which has two columns, x and y. I wonder how to convert the data frame into a list of coordinates like this [(1,1),(2,2),(3,3)...]

Upvotes: 0

Views: 281

Answers (2)

akrun
akrun

Reputation: 887158

An option with split:

split(t(df), row(df))

Upvotes: 1

Paul
Paul

Reputation: 9087

asplit can be used to split a dataframe into rows.

df <- data.frame(x = runif(5), y = runif(5))
df
#>            x          y
#> 1 0.04174615 0.66301314
#> 2 0.50167904 0.04072988
#> 3 0.89908163 0.64645679
#> 4 0.47145695 0.99351128
#> 5 0.70795517 0.25947328

asplit(df, 1)
#> [[1]]
#>          x          y 
#> 0.04174615 0.66301314 
#> 
#> [[2]]
#>          x          y 
#> 0.50167904 0.04072988 
#> 
#> [[3]]
#>         x         y 
#> 0.8990816 0.6464568 
#> 
#> [[4]]
#>         x         y 
#> 0.4714569 0.9935113 
#> 
#> [[5]]
#>         x         y 
#> 0.7079552 0.2594733 

Upvotes: 3

Related Questions