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

为君一笑 提交于 2019-11-28 09:32:15

问题


This question already has an answer here:

  • Split data frame string column into multiple columns 14 answers

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?


回答1:


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"), "\\(([^,]+), ([^)]+)\\)")



回答2:


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



回答3:


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")


来源:https://stackoverflow.com/questions/32042621/how-to-split-column-into-two-in-r-using-separate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!