How to split column into two in R using separate [

2020-02-14 10:48发布

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?

标签: r tidyr
3条回答
爷的心禁止访问
2楼-- · 2020-02-14 11:23

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"), "\\(([^,]+), ([^)]+)\\)")
查看更多
够拽才男人
3楼-- · 2020-02-14 11:35

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
查看更多
爷的心禁止访问
4楼-- · 2020-02-14 11:39

Alternatively, you could take numbers and create a data frame using the stringi package.

library(stringi)

data.frame(lat = stri_extract_first(mydf$x, regex = "\\d{1,}.\\d{1,}"),
           lon = stri_extract_last(mydf$x, regex = "\\d{1,}.\\d{1,}"))

#           lat          lon
#1 41.797634883 87.708426986
#2 41.911390159 87.732635428
#3 41.672925444 87.642819748
#4 41.759925265 87.698867528
#5 41.856122914 87.717449534
#6 41.900794625 87.671240384

Data

mydf <- structure(list(x = structure(c(3L, 6L, 1L, 2L, 4L, 5L), .Label = c("(41.672925444, -87.642819748)", 
"(41.759925265, -87.698867528)", "(41.797634883, -87.708426986)", 
"(41.856122914, -87.717449534)", "(41.900794625, -87.671240384)", 
"(41.911390159, -87.732635428)"), class = "factor")), .Names = "x", row.names = c(NA, 
-6L), class = "data.frame")
查看更多
登录 后发表回答