Turning a string of numbers into a list of numbers in R

后端 未结 4 490
渐次进展
渐次进展 2021-01-21 05:09

Apologies if this question is too easy, I know how to do it in Python but I currently need it in R.

As part of an SQL query I get a variable with some numbers (the lengt

相关标签:
4条回答
  • 2021-01-21 05:32

    We can extract the first list elements and convrert to numeric

    library(stringr)
    as.numeric(str_extract_all(x, "[0-9.]+")[[1]])
    #[1] 0.50 0.25 0.75 0.50
    

    Or with base R using regmatches/regexpr

    as.numeric(regmatches(x, gregexpr("[0-9.]+", x))[[1]])
    #[1] 0.50 0.25 0.75 0.50
    

    Or with scan after removing the curly brackets

    scan(text= gsub("[{}]", "", x), what = numeric(), sep="," , quiet = TRUE)
    
    0 讨论(0)
  • 2021-01-21 05:41

    You can try using gsub to first replace { and } and then split in vector using strsplit. Finally, convert it to numeric as:

    x <- "{0.5,0.25,0.75,0.5}" 
    as.numeric(strsplit(gsub("[{}]","",x), split = ",")[[1]])
    #[1] 0.50 0.25 0.75 0.50
    
    0 讨论(0)
  • 2021-01-21 05:43

    You can do this in base R as

    as.numeric(strsplit(substr(x, 2, nchar(x) - 1), ',')[[1]])
    

    or

    as.numeric(strsplit(gsub('[{]|[}]', '', x), ',')[[1]])
    
    0 讨论(0)
  • 2021-01-21 05:58

    You can also use scan :

    scan(text=substr(x,2,nchar(x)-1),sep=",")
    [1] 0.50 0.25 0.75 0.50
    

    Not sure if performance is a concern but I was curious so here's a benchmark:

    on longer string:

    x <- paste0("{",paste(1:1e4,collapse=","),"}")
    
    as.numeric(str_extract_all(x, "[0-9.]+")[[1]])
    library(stringr)
    microbenchmark::microbenchmark(
    ak1 = as.numeric(str_extract_all(x, "[0-9.]+")[[1]]),
    ak2 = as.numeric(regmatches(x, gregexpr("[0-9.]+", x))[[1]]),
    ak3 = scan(text= gsub("[{}]", "", x), what = numeric(), sep="," , quiet = TRUE),
    mkr = as.numeric(strsplit(gsub("[{}]","",x), split = ",")[[1]]),
    sat = as.numeric(unlist( strsplit( gsub("[^0-9.,]", "", x), ",") ) ),
    ry1 = as.numeric(strsplit(substr(x, 2, nchar(x) - 1), ',')[[1]]),
    ry2 = as.numeric(strsplit(gsub('[{]|[}]', '', x), ',')[[1]]),
    mm  = scan(text=substr(x,2,nchar(x)-1),sep=",", quiet = TRUE),
    unit = "relative" 
    )
    
    # Unit: relative
    # expr       min        lq      mean    median        uq       max neval
    # ak1  1.083862  1.081196  1.024354  1.075517  1.056627 0.3696952   100
    # ak2 20.581096 19.829962 18.775549 19.599953 19.307974 5.7053902   100
    # ak3  1.309869  1.313783  1.258867  1.314094  1.322486 0.3918785   100
    # mkr  2.817353  2.765637  2.682597  2.761487  2.719283 0.9331140   100
    # sat  2.908291  2.871177  2.784193  2.871431  2.815423 1.4278423   100
    # ry1  2.521181  2.463614  2.329599  2.456323  2.423078 0.6853562   100
    # ry2  2.932874  2.859785  2.778728  2.865958  2.828777 0.8790090   100
    #  mm  1.000000  1.000000  1.000000  1.000000  1.000000 1.0000000   100
    

    on original short string:

    # Unit: relative
    # expr      min    lq     mean   median    uq      max neval
    #  ak1 2.183908 2.520 2.513167 2.445887 2.464 4.383178   100
    #  ak2 3.574713 3.625 3.573718 3.432900 3.412 6.752336   100
    #  ak3 5.114943 4.860 4.746448 4.532468 4.620 5.981308   100
    #  mkr 1.425287 1.360 1.344941 1.285714 1.336 1.355140   100
    #  sat 1.873563 1.810 1.783697 1.753247 1.736 2.121495   100
    #  ry1 1.000000 1.000 1.000000 1.000000 1.000 1.000000   100
    #  ry2 1.471264 1.415 1.359581 1.354978 1.336 1.074766   100
    #  mm  4.390805 4.400 4.314622 4.134199 4.224 6.682243   100
    
    0 讨论(0)
提交回复
热议问题