[tech-spec] Today's R finger-exercise

  • From: Steve Wisdom <swisdom@xxxxxxxxx>
  • To: tech-spec@xxxxxxxxxxxxx
  • Date: Mon, 6 Dec 2004 12:01:25 -0500

--
Today's R finger-exercise 

I'll make this a continuing series, for the benefit (?) of the
beginners on the list

Today I was mucking around with a script to manipulate our position
sheets and wanted to change data of the form "Z4" or "H6", ie
contract-month codes, into a nice sortable format "200412" or "200603"

Variable "Code" is the raw, and here's what I wrote, using a little
hack from Venables, http://snipurl.com/b5j4

--

Code

paste(
as.numeric(substring(Code,2))+2000,
substring(100+match(substring(Code,1,1),unlist(strsplit("FGHJKMNQUVXZ",""))),2),
sep='')

--

> Code
  [1] "Z4" "Z4" "Z4" "Z4" "Z4" "Z4" "Z4" "Z4" "Z4" "Z4" "Z4" "F5" "F5"
"F5" "F5" "F5" "F5" "F5"
 [19] "F5" "F5" "F5" "F5" "F5" "F5" "F5" "F5" "G5" "G5" "G5" "G5" "G5"
"G5" "G5" "G5" "G5" "G5"
 [37] "G5" "G5" "G5" "H5" "H5" "H5" "H5" "H5" "H5" "H5" "H5" "H5" "H5"
"H5" "H5" "H5" "H5" "H5"
 [55] "H5" "H5" "H5" "H5" "H5" "H5" "H5" "H5" "M5" "M5" "M5" "M5" "M5"
"M5" "M5" "M5" "M5" "M5"
 [73] "M5" "M5" "M5" "M5" "M5" "M5" "M5" "M5" "M5" "M5" "M5" "M5" "M5"
"M5" "M5" "M5" "M5" "U5"
 [91] "U5" "U5" "U5" "U5" "U5" "U5" "U5" "U5" "U5" "U5" "U5" "U5" "U5"
"Z5" "Z5" "Z5" "Z5" "Z5"
[109] "Z5" "Z5" "Z5" "Z5" "Z5" "Z5" "Z5" "Z5" "Z5" "H6" "H6" "H6" "H6"
"H6" "H6" "M6" "M6" "M6"
[127] "M6" "M6" "M6" "M6"
> 
> paste(
+ as.numeric(substring(Code,2))+2000,
+ 
substring(100+match(substring(Code,1,1),unlist(strsplit("FGHJKMNQUVXZ",""))),2),
+ sep='')
  [1] "200412" "200412" "200412" "200412" "200412" "200412" "200412"
"200412" "200412" "200412"
 [11] "200412" "200501" "200501" "200501" "200501" "200501" "200501"
"200501" "200501" "200501"
 [21] "200501" "200501" "200501" "200501" "200501" "200501" "200502"
"200502" "200502" "200502"
 [31] "200502" "200502" "200502" "200502" "200502" "200502" "200502"
"200502" "200502" "200503"
 [41] "200503" "200503" "200503" "200503" "200503" "200503" "200503"
"200503" "200503" "200503"
 [51] "200503" "200503" "200503" "200503" "200503" "200503" "200503"
"200503" "200503" "200503"
 [61] "200503" "200503" "200506" "200506" "200506" "200506" "200506"
"200506" "200506" "200506"
 [71] "200506" "200506" "200506" "200506" "200506" "200506" "200506"
"200506" "200506" "200506"
 [81] "200506" "200506" "200506" "200506" "200506" "200506" "200506"
"200506" "200506" "200509"
 [91] "200509" "200509" "200509" "200509" "200509" "200509" "200509"
"200509" "200509" "200509"
[101] "200509" "200509" "200509" "200512" "200512" "200512" "200512"
"200512" "200512" "200512"
[111] "200512" "200512" "200512" "200512" "200512" "200512" "200512"
"200603" "200603" "200603"
[121] "200603" "200603" "200603" "200606" "200606" "200606" "200606"
"200606" "200606" "200606"
>

Other related posts:

  • » [tech-spec] Today's R finger-exercise