在R中,如何拆分长字符

在R中,如何拆分长字符,r,split,R,Split,在R中,有一个长字符“mr”作为blow,如何按数字拆分“mr”(拆分为三个短字符串): mr在@rawr的评论中添加, 如果要将其作为数据帧 mr <- 'total amount 25.36 expense -2 promotion discount-2.56' splt <- strsplit(mr, '(?<=\\d) ', perl = TRUE)[[1]] df <- data.frame("Desciption" = gsub("

在R中,有一个长字符“mr”作为blow,如何按数字拆分“mr”(拆分为三个短字符串):


mr在@rawr的评论中添加,
如果要将其作为数据帧

mr <- 'total amount 25.36 expense -2 promotion discount-2.56'
splt <- strsplit(mr, '(?<=\\d) ', perl = TRUE)[[1]]
df <- data.frame("Desciption" = gsub("[^a-z ]", "", splt),
           "Amount" = as.numeric(gsub("[^0-9.-]", "", splt)))
df
          Desciption Amount
1      total amount   25.36
2           expense   -2.00
3 promotion discount  -2.56

mr带有
tidyverse的选项

library(dplyr)
library(tidyr)
tibble(col1 = mr) %>% 
   separate_rows(col1, sep="(?<=\\d) ") %>%
   separate(col1, into = c("Description", "Amount"),
           sep = "(?<=[a-z])\\s*(?=[-0-9])", convert = TRUE)
# A tibble: 3 x 2
#  Description        Amount
#   <chr>               <dbl>
#1 total amount        25.4 
#2 expense             -2   
#3 promotion discount  -2.56
库(dplyr)
图书馆(tidyr)
TIBLE(col1=mr)%>%

分开的行(col1,sep=“(?你说的“按数字分割”是什么意思
strsplit(mr)”(?这很有用,thanks@anderwyang如果有效,请考虑检查。
library(dplyr)
library(tidyr)
tibble(col1 = mr) %>% 
   separate_rows(col1, sep="(?<=\\d) ") %>%
   separate(col1, into = c("Description", "Amount"),
           sep = "(?<=[a-z])\\s*(?=[-0-9])", convert = TRUE)
# A tibble: 3 x 2
#  Description        Amount
#   <chr>               <dbl>
#1 total amount        25.4 
#2 expense             -2   
#3 promotion discount  -2.56