earnestatistics
earnestatistics

Reputation: 132

Convert string to vector

I have the following character:

> a
[1] "01'='AS','02'='BC','03'='BS','04'='CC','05'='CS','06'='CH','07'='CL','08'='CM','09'='DF','10'='DG','11'='GT','12'='GR','13'='HG','14'='JC','15'='MC','16'='MN','17'='MS','18'='NT','19'='NL','20'='OC','21'='PL','22'='QT','23'='QR','24'='SP','25'='SL','26'='SR','27'='TC','28'='TL','29'='TS','30'='VZ','31'='YN','32'='ZS"

I wish to convert to a vector like this one:

>translate<-c('01'='AS','02'='BC','03'='BS','04'='CC','05'='CS','06'='CH','07'='CL','08'='CM','09'='DF','10'='DG','11'='GT','12'='GR','13'='HG','14'='JC','15'='MC','16'='MN','17'='MS','18'='NT','19'='NL','20'='OC','21'='PL','22'='QT','23'='QR','24'='SP','25'='SL','26'='SR','27'='TC','28'='TL','29'='TS','30'='VZ','31'='YN','32'='ZS')

So then I would have this as a result:

>  translate

>   01   02   03   04   05   06   07   08   09   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24 
"AS" "BC" "BS" "CC" "CS" "CH" "CL" "CM" "DF" "DG" "GT" "GR" "HG" "JC" "MC" "MN" "MS" "NT" "NL" "OC" "PL" "QT" "QR" "SP" 
  25   26   27   28   29   30   31   32 
"SL" "SR" "TC" "TL" "TS" "VZ" "YN" "ZS" 

Right now my only way to solve this is by using copy paste on the output from "a" and writing c(' copy -paste a '). I wish to do it in a general way. I'm using windows 7operating system and Rstudio 3.0.2

Upvotes: 1

Views: 297

Answers (3)

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

Here's a short approach:

s <- regmatches(a, gregexpr("\\w+", a))[[1]]
translate <- setNames(s[c(FALSE, TRUE)], s[c(TRUE, FALSE)])

Result:

  01   02   03   04   05   06   07   08   09   10   11   12   13   14 
"AS" "BC" "BS" "CC" "CS" "CH" "CL" "CM" "DF" "DG" "GT" "GR" "HG" "JC" 
  15   16   17   18   19   20   21   22   23   24   25   26   27   28 
"MC" "MN" "MS" "NT" "NL" "OC" "PL" "QT" "QR" "SP" "SL" "SR" "TC" "TL" 
  29   30   31   32 
"TS" "VZ" "YN" "ZS"

Upvotes: 1

Roland
Roland

Reputation: 132596

I might actually use the dreaded eval(parse(...)) for this:

a <- "01'='AS','02'='BC"

translate <- eval(parse(text=paste0("c('", a, "')")))

#  01   02 
#"AS" "BC" 

However, why do you have this string?

Upvotes: 4

eddi
eddi

Reputation: 49448

a = "01'='AS','02'='BC','03'='BS','04'='CC','05'='CS','06'='CH','07'='CL','08'='CM','09'='DF','10'='DG','11'='GT','12'='GR','13'='HG','14'='JC','15'='MC','16'='MN','17'='MS','18'='NT','19'='NL','20'='OC','21'='PL','22'='QT','23'='QR','24'='SP','25'='SL','26'='SR','27'='TC','28'='TL','29'='TS','30'='VZ','31'='YN','32'='ZS"

tmp = strsplit(a, split = "','|'='")[[1]]

translate = tmp[seq(2, length(tmp), 2)]
names(translate) = tmp[seq(1, length(tmp), 2)]
translate
#  01   02   03   04   05   06   07   08   09   10   11   12   13   14   15   16   17   18   #19   20   21   22 
#"AS" "BC" "BS" "CC" "CS" "CH" "CL" "CM" "DF" "DG" "GT" "GR" "HG" "JC" "MC" "MN" "MS" "NT" #"NL" "OC" "PL" "QT" 
#  23   24   25   26   27   28   29   30   31   32 
#"QR" "SP" "SL" "SR" "TC" "TL" "TS" "VZ" "YN" "ZS" 

Each individual step is pretty simple - run separately to see what each one does.

Upvotes: 2

Related Questions