如何使用单独的R将列分成两列 [英] How to split column into two in R using separate

查看:28
本文介绍了如何使用单独的R将列分成两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含这样一列位置的数据集 (41.797634883, -87.708426986).我想把它分成纬度和经度.我尝试使用 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'))

但我收到此错误

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

我做错了什么?

推荐答案

指定分隔符

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

但是,extract 看起来更干净,因为您可以同时删除()"

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

dataframe %>% extract(x, c("Latitude", "Longitude"), "\\(([^,]+), ([^)]+)\\)")

这篇关于如何使用单独的R将列分成两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆