Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split column into two in R using separate [duplicate]

Tags:

r

tidyr

I have a dataset with a column of locations like this (41.797634883, -87.708426986). I'm trying to split it into latitude and longitude. I tried using the separate method from the tidyr package

library(dplyr)
library(tidyr)
df <- data.frame(x = c('(4, 9)', '(9, 10)', '(20, 100)', '(100, 200)'))
df %>% separate(x, c('Latitude', 'Longitude'))

but I'm getting this error

Error: Values not split into 2 pieces at 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 

What am I doing wrong?

like image 946
ytk Avatar asked Nov 30 '22 17:11

ytk


2 Answers

Specify the separating character

dataframe %>% separate(Location, c('Latitude', 'Longitude'), sep=",")

But, extract looks cleaner for this since you can remove the "()" at the same time

dataframe %>% extract(x, c("Latitude", "Longitude"), "\\(([^,]+), ([^)]+)\\)")
like image 90
Rorschach Avatar answered Dec 05 '22 13:12

Rorschach


You can use base R to do this. Remove the parentheses with gsub and use read.table to read the column 'x' (based on @jazzuro's example) to separate into two columns.

 read.table(text=gsub('[()]', '', mydf$x), 
         sep=",", col.names=c('Latitute', 'Longitude'))
 #   Latitute Longitude
 #1 41.79763 -87.70843
 #2 41.91139 -87.73264
 #3 41.67293 -87.64282
 #4 41.75993 -87.69887
 #5 41.85612 -87.71745
 #6 41.90079 -87.67124
like image 44
akrun Avatar answered Dec 05 '22 12:12

akrun