将文本拆分为字符和数字
splitting text into character and numeric
谁能帮我拆分这个字符串:
string <- "Rolling in the deep .25"
我试图从中得到两个输出:
1) Rolling in the Deep # character
2) 15.25 # numeric value
我知道如何在 excel 中做到这一点,但在 R
中有点迷茫
使用 strsplit
即可。解决方案如下:
string <- "Rolling in the deep .25"
strsplit(string, "\s+\$")
^ ^___ find a $ (escaped with \ because $ means end of word)
\______ find 1 or more whitespaces
# Result
#"Rolling in the deep" "15.25"
strsplit(string, "\s+\$")[[1]][1]
#[1] "Rolling in the deep"
strsplit(string, "\s+\$")[[1]][2]
#[1] "15.25"
只要右侧总是以美元符号开头,您就需要 "escape" 美元符号。试试这个:
# you will need stringr, which you could load alone but the tidyverse is amazing
library(tidyverse)
string <- "Rolling in the deep .25"
str_split_fixed(string, "\$", n = 2)
以下是仅使用正则表达式提取信息的方法:
x <- c("Rolling in the deep .25",
"Apetite for destruction .00",
"Piece of mind ")
rgx <- "^(.*)\s{2,}(\$.*)$"
data.frame(album = trimws(gsub(rgx, "\1", x)),
price = trimws(gsub(rgx, "\2", x))
)
album price
1 Rolling in the deep .25
2 Apetite for destruction .00
3 Piece of mind
谁能帮我拆分这个字符串:
string <- "Rolling in the deep .25"
我试图从中得到两个输出:
1) Rolling in the Deep # character
2) 15.25 # numeric value
我知道如何在 excel 中做到这一点,但在 R
中有点迷茫使用 strsplit
即可。解决方案如下:
string <- "Rolling in the deep .25"
strsplit(string, "\s+\$")
^ ^___ find a $ (escaped with \ because $ means end of word)
\______ find 1 or more whitespaces
# Result
#"Rolling in the deep" "15.25"
strsplit(string, "\s+\$")[[1]][1]
#[1] "Rolling in the deep"
strsplit(string, "\s+\$")[[1]][2]
#[1] "15.25"
只要右侧总是以美元符号开头,您就需要 "escape" 美元符号。试试这个:
# you will need stringr, which you could load alone but the tidyverse is amazing
library(tidyverse)
string <- "Rolling in the deep .25"
str_split_fixed(string, "\$", n = 2)
以下是仅使用正则表达式提取信息的方法:
x <- c("Rolling in the deep .25",
"Apetite for destruction .00",
"Piece of mind ")
rgx <- "^(.*)\s{2,}(\$.*)$"
data.frame(album = trimws(gsub(rgx, "\1", x)),
price = trimws(gsub(rgx, "\2", x))
)
album price
1 Rolling in the deep .25
2 Apetite for destruction .00
3 Piece of mind