Grep文字并放入数据框

时间:2019-04-19 06:55:05

标签: r dataframe

通过checkresiduals()包中的forecast函数,我得到了以下结果:

#Result of checkresiduals() function
test <- "Q* = 4.5113, df = 4.6, p-value = 0.4237"

现在,我的意图是使用grep()或其他功能将这一行文本拆分为一个data.frame(具有三列Q*dfp-value) ,如下面的示例所示:

    Q*         df       p-value
    4.5113     4.6      0.4237

有人可以用这个代码帮助我吗?

3 个答案:

答案 0 :(得分:3)

您可以使用 ; roots.asm segment ;gcc.text global -c rootsc._roots _roots: enter 0,0 ;create stack frame for procedure parameters xor EAX,EAX ;EAX = 0 fld qword[EBP+8] ; load floating point value : a fadd ST0 ; 2a fld qword[EBP+8] ; a,2a fld qword[EBP+24] ; c,a,2a ;nasm fmulp ST1 ; ac,2a fadd ST0 ; 2ac,2a fadd st0 ; 4ac,2a fchs ; -f4ac,2a elf64 fld qword[EBP+16] ; b,-g4ac,2a fld qword[EBP+16] ; b,b,-F4ac,2a stabs fmulp ST1 ; b*b,-o4ac,2a roots.o roots.asm faddp ST1 ; b*b-4ac,2a ;gcc rootsc.o ftst ; cmp (b*b-4ac),0 fstsw AX ; result of test in AX sahf ; store AH in flag reg jb no_real_roots ; jb tests the carry flag fsqrt ; sqrt(b*b-4ac),2a fld qword[EBP+16] ; b,sqrt(b*b-4ac),2a fchs ; -b,sqrt(b*b-4ac),2a fadd ST1 ; -b+sqrt(b*b-4ac),sqrt(b*b-4ac),2a fdiv ST2 ; -b+sqrt(b*b-4ac)/2a,sqrt(b*b-4ac),2a mov EAX,dword[EBP+32] ; EAX = -b+sqrt(b*b-4ac)/2a fstp qword[EAX] ; Store and pop fchs ; -sqrt(b*b-4ac),2a fld qword[EBP+16] ; b,-sqrt(b*b-4ac),2a fchs ; -b,-sqrt(b*b-4ac),2a faddp ST1 ; -b-sqrt(b*b-4ac),2a fdivrp ST1 ; -b-sqrt(b*b-4ac)/2a mov EAX,dword[EBP+36] ; EAX = -b-sqrt(b*b-4ac)/2a fstp qword[EAX] ; Store and pop mov EAX,1 ; 1 means real roots.o jmp short done no_real_roots: fchs ; Make b*b-o4ac positive fsqrt ; sqrt(b*b-4ac),2a fld qword[EBP+16] ; b,sqrt(b*b-4ac),2a fchs ; -b,sqrt(b*b-4ac),2a fadd ST1 ; -b+sqrt(b*b-4ac),sqrt(b*b-4ac),2a fdiv ST2 ; -b+sqrt(b*b-4ac)/2a,sqrt(b*b-4ac),2a mov EAX,dword[EBP+32] ; EAX = -b+sqrt(b*b-4ac)/2a fstp qword[EAX] ; Store and pop fchs ; -sqrt(b*b-4ac),2a fld qword[EBP+16] ; b,-sqrt(b*b-4ac),2a fchs ; -b,-sqrt(b*b-4ac),2a faddp ST1 ; -b-sqrt(b*b-4ac),2a fdivrp ST1 ; -b-sqrt(b*b-4ac)/2a mov EAX,dword[EBP+36] ; EAX = -b-sqrt(b*b-4ac)/2a fstp qword[EAX] ; Store and pop sub EAX,EAX ; 0 means no real roots done: leave ret

strsplit

答案 1 :(得分:2)

这是tidyverse

的一种方法
library(tidyverse)
tibble(test) %>% 
    separate_rows(test, sep = ",\\s*") %>% 
    separate(test, into = c("v1", 'v2'), sep= " = ") %>% 
    deframe %>%
    as.list %>% 
    as_tibble
# A tibble: 1 x 3
#  `Q*`   df    `p-value`
#  <chr>  <chr> <chr>    
#1 4.5113 4.6   0.4237   

还可以将其修改为JSON,并使用jsonlite轻松阅读。

library(jsonlite)
data.frame(fromJSON(paste0("{", gsub('([^0-9, ]+)(?: \\=)', '"\\1":', 
               test), "}")), check.names = FALSE) 
#       Q*  df p-value
#1 4.5113 4.6  0.4237

答案 2 :(得分:1)

这里有两种替代方法:

  1. 将字符串转换为DCF格式并使用read.dcf()
  2. “根据语言进行计算”:将字符串转换为有效的R表达式并使用parse() / eval()

read.dcf()

在将字符串read.dcf()转换为DCF(Debian控制文件)格式之后,使用test函数。
(顺便说一句,每个R包的DESCRIPTION文件都是DCF格式。)

library(magrittr) # piping used for readability
test %>% 
  stringr::str_replace_all("=", ":") %>%        # replace "=" by ":"
  stringr::str_replace_all(",\\s*", "\n") %>%   # replace ", " by line break
  textConnection() %>% 
  read.dcf(all = TRUE)
     Q*  df  p-value 
1 4.5113 4.6   0.4237

所有列均为字符类型。

使用语言进行计算

library(magrittr) # piping used for readability
test %>%   
  stringr::str_replace_all("(\\S+) =", "`\\1` =") %>% 
  paste0("data.frame(", ., ", check.names = FALSE)") %>% 
  parse(text = .) %>% 
  eval()
      Q*  df p-value
1 4.5113 4.6  0.4237

所有列的类型均为double。

test %>%   
  stringr::str_replace_all("(\\S+) =", "`\\1` =") %>%   
  paste0("data.frame(", ., ", check.names = FALSE)")

返回

"data.frame(`Q*` = 4.5113, `df` = 4.6, `p-value` = 0.4237, check.names = FALSE)"

然后将其解析为表达式并求值。

请注意,所有变量名都用引号引起来处理语法上无效的变量名,例如Q*p-value