有人可以帮我分割这个字符串:
string <- "Rolling in the deep $15.25"
我试图从中得到两个输出:
1) Rolling in the Deep # character
2) 15.25 # numeric value
我知道如何在excel中做到这一点,但R有点失落
使用strsplit
就可以了。解决方案如下:
string <- "Rolling in the deep $15.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"
只要右手边总是有一个美元符号,你就需要“逃脱”美元符号。试试这个:
# you will need stringr, which you could load alone but the tidyverse is amazing
library(tidyverse)
string <- "Rolling in the deep $15.25"
str_split_fixed(string, "\\$", n = 2)
以下是使用正则表达式提取信息的方法:
x <- c("Rolling in the deep $15.25",
"Apetite for destruction $20.00",
"Piece of mind $19")
rgx <- "^(.*)\\s{2,}(\\$.*)$"
data.frame(album = trimws(gsub(rgx, "\\1", x)),
price = trimws(gsub(rgx, "\\2", x))
)
album price
1 Rolling in the deep $15.25
2 Apetite for destruction $20.00
3 Piece of mind $19