如何将包含关系运算符的字符串向量转换为区间向量?

时间:2017-03-09 06:14:17

标签: r

我有一个数据集,其中一列应该是数值,但有些行的值包含关系运算符,如'> ='或'< ='。为了简化这一点,请考虑我有一个向量:

    .data
height:     .asciiz     "enter your height: "
weight:     .asciiz     "enter your weight: "
little:     .float      18.5
medium:     .float      25
large:      .float      30
fpconstant: .float      703
bmi:        .float
under:      .asciiz     " This is considered underweight"
health:     .asciiz     " This is considered healthy"
overweight: .asciiz     " This is considered overweight"
obese:      .asciiz     " This is considered obese."
name:       .asciiz     ", here are your BMI results\n"
prompt:     .asciiz     "Please enter your name: "
buffer:     .space      20

    .text

main:
    li      $v0,4
    la      $a0,prompt
    syscall

    j       fix                     # skip over [broken] original code

    # ------------------------------------------------------------------------
    # NOTE/BUG: here is the original code
    li      $v0,8
    syscall

    la      $a0,buffer              # load byte space into address

    li      $a1,20                  # allot the byte space for string
    move    $t6,$a0                 # save string to t6
    syscall

    li      $v0,8
    syscall
    # ------------------------------------------------------------------------

    # ------------------------------------------------------------------------
    # NOTE/FIX: here is the fixed code
fix:
    la      $a0,buffer              # get address of name buffer
    li      $a1,20                  # allot the byte space for string
    li      $v0,8
    syscall
    move    $t6,$a0                 # save string to t6

    # strip newline for pretty print
strip:
    lbu     $t0,0($a0)              # get char
    addiu   $a0,$a0,1               # advance to next char
    beqz    $t0,strip_done          # EOS? if yes, done
    bne     $t0,0x0A,strip          # newline? if no, loop
    sb      $zero,-1($a0)           # strip newline
strip_done:

    # ------------------------------------------------------------------------

    li      $v0,4
    la      $a0,weight
    syscall

    li      $v0,6
    syscall

    # f1 now contains weight, will continue after this correction has been made.
    movf.s  $f1,$f0
    l.s     $f2,fpconstant
    mul.s   $f0,$f1,$f2

    movf.s  $f1,$f0
    movf.s  $f12,$f0

    li      $v0,4
    la      $a0,height
    syscall

    li      $v0,6
    syscall

    # f2 now contains height, will continue after this correction has been made.
    movf.s  $f2,$f0
    mul.s   $f0,$f2,$f2
    movf.s  $f2,$f0
    movf.s  $f12,$f0

    div.s   $f3,$f1,$f2             # f3 is our BMI
    movf.s  $f12,$f3

    la      $a0,buffer              # reload byte space to primary address
    move    $a0,$t6                 # primary address = t6 (load pointer)
    li      $v0,4                   # print string

    # NOTE: this is _not_ the true source of the problem -- see above
    syscall                         # Problem

    la      $a0,name
    move    $t0,$a0
    syscall

    li      $v0,2
    syscall

    l.s     $f4,little
    c.lt.s  $f3,$f4

    # Branch if specified FP condition flag true (BC1T, not BCLT) :
    # If Coprocessor 1 condition flag specified by immediate is true (one) then
    # branch to statement at label's address
    # Compare less than single precision :
    # If $f0 is less than $f1, set Coprocessor 1 condition flag 0 true
    # else set it false
    bc1t    underweight

    l.s     $f4,medium
    c.lt.s  $f3,$f4
    bc1t    mid

    l.s     $f4,large
    c.lt.s  $f3,$f4
    bc1t    big

    li      $v0,4
    la      $a0,obese
    j       result

underweight:
    la      $a0,under
    j       result

mid:
    la      $a0,health
    j       result

big:
    la      $a0,overweight
    j       result

result:
    li      $v0,4
    syscall

    li      $v0,10
    syscall

我应该如何将其转换为间隔的数字向量,如:

$header = $section->createHeader();
$header->addImage('/home/dev238/projects/aegis/images/Logo_.jpg',['align'=>'right']);

3 个答案:

答案 0 :(得分:0)

我们可以使用parse_number

library(readr)
parse_number(a)
#[1] 0.51 0.33 0.99 0.66 0.01

答案 1 :(得分:0)

使用正则表达式和函数gsub():

a <- c('.51', '.33', '> .99', '.66', '<= .01')
a.n <- gsub("<|>|=| ", "", a)
n <- as.numeric(a.n)
n
[1] 0.51 0.33 0.99 0.66 0.01

请参阅?regexp或gsub以获取更多帮助。

答案 2 :(得分:0)

我想也许您可以使用切换来解决您的问题。

a <- c('>= 0.99', '= 0.66', '<= 0.01')
oper_num <- matrix(unlist(strsplit(a," "), recursive = T), ncol=2, byrow = T)
limits <- function(vec){
  operation = vec[1]
  number = vec[2]
  switch(
    operation,
    ">=" = paste0("[", number,", Inf]"),
    ">" =paste0("(", number, ", Inf]"),
    "=" = paste0("[", number, ", ", number, "]"),
    "<" = paste0("[-Inf, ", number, ")"),
    "<=" =paste0("[-Inf, ", number, "]")
  )
}
apply(oper_num, 1, limits)
#> [1] "[0.99, Inf]"    "[0.66, 0.66]"   "[-Inf, 0.01]"