Search code examples
rcharactercoercion

How to convert a string with numbers and spaces in between into a number string?


Say that I have a number string which I've copied and pasted from the internet.

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00

I'm trying to convert this number string into a workable format in R

I've tried converting to character and then replacing the spacing with commas, but that produces

gridinput<-"08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00"

gridtrim<-(gsub("[\r\n]",",",(gsub("[[:blank:]]+", ",",
                                                  gridinput))))
gridtrim

[1] "08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08,49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00"

Which is still one character string.

I know I can convert to as.numeric if I can get quotation marks around each double digit, but I don't know how to do this.

Any help?


Solution

  • Using scan.

    scan(text=gridinput, qui=T)
    # [1]  8  2 22 97 38 15  0 40  0 75  4  5  7 78 52 12 50 77 91  8 49 49
    # [23] 99 40 17 81 18 57 60 87 17 40 98 43 69 48  4 56 62  0
    

    or if you want the numbers as characters:

    scan(text=gridinput, what=character(), qui=T)
    # [1] "08" "02" "22" "97" "38" "15" "00" "40" "00" "75" "04" "05" "07"
    # [14] "78" "52" "12" "50" "77" "91" "08" "49" "49" "99" "40" "17" "81"
    # [27] "18" "57" "60" "87" "17" "40" "98" "43" "69" "48" "04" "56" "62"
    # [40] "00"
    

    Alternatively strsplit:

    as.numeric(unlist(strsplit(gridinput, ' ')))       
    unlist(strsplit(gridinput, ' '))