我有一个位置列,如下所示 -
Location
San Jose CA
Santa Clara CA
我想分成两列。我正在使用来自tidyr的单独函数,但是将sep参数作为“”给出了以下内容 -
City State
San Jose
Santa Clara
如何仅根据第二个空格拆分列?
structure(list(Location = c("San Jose CA", "Santa Clara CA")),
.Names = "Location", class = "data.frame", row.names = c(NA, -2L))
答案 0 :(得分:4)
library(tidyr)
df <- data.frame(Location = c("San Jose CA", "Fremont CA", "Santa Clara CA"))
separate(df, Location, into = c("city", "state"), sep = " (?=[^ ]+$)")
# city state
# 1 San Jose CA
# 2 Fremont CA
# 3 Santa Clara CA
答案 1 :(得分:0)
使用base R
的选项涉及使用,
替换最后一个空格(使用sub
),然后使用read.csv
来读取该列。
read.csv(text=sub("\\s+(\\S+)$", ",\\1", df1$Location),
col.names=c('City', 'State'), header=FALSE)
# City State
#1 San Jose CA
#2 Santa Clara CA