且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用R中的最后两个字符分割字符串? (/负字符串索引)

更新时间:2023-11-15 14:02:46

tidyr :: separate 通过允许您传递拆分位置的整数索引(包括从字符串末尾开始的负索引),使分隔列变得简单。 (当然,正则表达式也起作用。)

tidyr::separate makes separating columns simple by allowing you to pass an integer index of split position, including negatively indexed from the end of the string. (Regex works as well, of course.)

library(tidyr)

b %>% separate(name, into = c('name', 'age'), sep = -4, convert = TRUE)
##   height        name age
## 1    190 John Smith   34
## 2    165  Mr.Turner   54
## 3    174 Antonio P.   23
## 4    176 John Brown   31

或以空格分隔:

b %>% separate(name, into = c('name', 'age'), sep = '\\s(?=\\S*?$)', convert = TRUE)

返回相同的内容。

在基数R中,还有更多工作:

In base R, it's a bit more work:

b$name <- as.character(b$name)
split_name <- strsplit(b$name, '\\s(?=\\S*?$)', perl = TRUE)
split_name <- do.call(rbind, split_name)
colnames(split_name) <- c('name', 'age')
b <- data.frame(b[-2], split_name, stringsAsFactors = FALSE)
b$age <- type.convert(b$age)

b
##   height       name age
## 1    190 John Smith  34
## 2    165  Mr.Turner  54
## 3    174 Antonio P.  23
## 4    176 John Brown  31