3

I have a string vector like

sv = c('xx_S1_xx', 'xx_S1_xx', 'xx_S2_xx', 'xx_S3_xx', ...)

But I have difficuly to find a dictionary-like structure so I can convert it to

map = { S1=>c('xx_S1_xx', 'xx_S1_xx'), S2=>c('xx_S2_xx'), S3=>c('xx_S3_xx'), ... }

Any suggestions?

Mavershang
  • 1,266
  • 2
  • 15
  • 27
  • Take a look [here](https://stackoverflow.com/questions/7818970/is-there-a-dictionary-functionality-in-r/44570412#44570412). The `hash` package is a possible way to go. – Stéphane Laurent Mar 02 '20 at 16:20
  • If you have a string vector in R you should make it look like a syntactically valid string vector in your question. `SV = c("xx_S1_xx","xx_S1_xx", "xx_S2_xx", "xx_S3_xx",...)` perhaps? – Spacedman Mar 02 '20 at 16:21

1 Answers1

5

in R a named list is the nearest thing to a dictionary or hashed array or whatever any other language calls it.

Construct with the list function and extract/assign elements with the $ operator:

> SLIST = list(S1=c("x_S1_x","x_S1_x"), S2="xx_S2_xx", S3="xx_S3_xx")
> SLIST$S1
[1] "x_S1_x" "x_S1_x"
Spacedman
  • 92,590
  • 12
  • 140
  • 224