Splitting the Values within a column in several columns in Rstudio

Viewed 17

I'm working with a little dataset in Rstudio, and it looks like this:

Id.Plac          ba geometry                             
   <chr>         <dbl> <chr>                                
 1 PL0000258578H 28.3  c(1027000.02095157, 6822000.02823209)
 2 PL0000258579I 48.9  c(1022000.03574538, 6879999.95813936)
 3 PL0000258580J 33.8  c(1022999.97026305, 6822999.97747748)
 4 PL0000258581K 48.9  c(1015000.01612418, 6776000.01983817)
 5 PL0000258582L 21.1  c(980999.971016645, 6795999.96903894)

I would like to split the Column 'geometry' , into 2 columns that refer to latitude and longitude columns, so that the final result looks more or less like :

Id.Plac          ba    X                 Y                
                              
 1 PL0000258578H 28.3  1027000.02095157  6822000.02823209
 2 PL0000258579I 48.9  1022000.03574538  6879999.95813936
 3 PL0000258580J 33.8  1022999.97026305  6822999.97747748
 4 PL0000258581K 48.9  1015000.01612418  6776000.01983817
 5 PL0000258582L 21.1  980999.971016645  6795999.96903894

Hope you guys can help me .. Thanks !

1 Answers

Using an eval parse approach.

options(digits=15)  ## to make digits visible

cbind(dat[-3], geo=t(sapply(dat$geometry, \(x) eval(parse(text=x)), USE.NAMES=FALSE)))
#         Id.Plac   ba             geo.1            geo.2
# 1 PL0000258578H 28.3 1027000.020951570 6822000.02823209
# 2 PL0000258579I 48.9 1022000.035745380 6879999.95813936
# 3 PL0000258580J 33.8 1022999.970263050 6822999.97747748
# 4 PL0000258581K 48.9 1015000.016124180 6776000.01983817
# 5 PL0000258582L 21.1  980999.971016645 6795999.96903894

Data:

dat <- structure(list(Id.Plac = c("PL0000258578H", "PL0000258579I", 
"PL0000258580J", "PL0000258581K", "PL0000258582L"), ba = c(28.3, 
48.9, 33.8, 48.9, 21.1), geometry = c("c(1027000.02095157, 6822000.02823209)", 
"c(1022000.03574538, 6879999.95813936)", "c(1022999.97026305, 6822999.97747748)", 
"c(1015000.01612418, 6776000.01983817)", "c(980999.971016645, 6795999.96903894)"
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5"
))
Related