Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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, ' '))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading