-1

I am trying to create a new character vector in R based on the input value present in 'operator' character vector. The operator variable contains values like ">", "<" "" and NULL. I need to create a new vector like operator_id which has equivalent numeric code for the above mathematical operators. Please find the code that I wrote using for loop. However this is very time consuming and is there any other efficient way of writing this code?

for (ch in operator){
  if (ch == ""){
    #print("hi")
    operator_concept_id = append(operator_concept_id, 4172703L)
    value_as_concept_id = append(value_as_concept_id, 45884084L)
  } else if (ch == ">"){
    #print("hello")
    operator_concept_id = append(operator_concept_id, 4172704L)
    value_as_concept_id = append(value_as_concept_id, 45876384L)
  } else if (ch == "<"){
    #print("less")
    operator_concept_id = append(operator_concept_id, 4171756L)
    value_as_concept_id = append(value_as_concept_id, 45881666L)
  }
  else if(ch== "-"){
    #print("negative")
    operator_concept_id = append(operator_concept_id, 4172703L)
    value_as_concept_id = append(value_as_concept_id, 45878583L)
  } else{
    #print("nothing")
    operator_concept_id = append(operator_concept_id, 0L)
    value_as_concept_id = append(value_as_concept_id, 45881630L)
  }
}
JJJ
  • 969
  • 6
  • 19
  • 30
The Great
  • 6,010
  • 5
  • 18
  • 62
  • Could you summarise your aim in one line? – NelsonGon Mar 15 '19 at 04:31
  • My aim is to create a new output character vector filled with numeric values corresponding to the input(operator) vector which has mathematical symbols. – The Great Mar 15 '19 at 04:33
  • What do you mean by corresponding? What is the relationship between the "operator" and values? – NelsonGon Mar 15 '19 at 04:34
  • Please take some time reviewing [how to ask](https://stackoverflow.com/help/how-to-ask) questions here on SO, and then provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), including sample data. Screenshots of your data/code are never a good idea, as we can't copy&paste from an image. You should provide sample data in a copy&paste-able format (e.g. use `dput`), show your expected output and give a clear reproducible and self-contained problem statement. – Maurits Evers Mar 15 '19 at 04:34
  • @NelsonGon - For example , ">" symbol can be represented as 4172704L in numeric form. Similarly "", NULL "-", etc – The Great Mar 15 '19 at 04:37

2 Answers2

2

Hopefully I got the aim right, this is a possible solution:

Operators<-c(">","<","NULL")#Did not use a real `NULL`
Numerics<-c(1234,567,8910)
purrr::map2(Operators,Numerics,function(x,y) append(x,y))

Result:

#[[1]]
#[1] ">"    "1234"

#[[2]]
#[1] "<"   "567"

#[[3]]
#[1] "NULL" "8910"
NelsonGon
  • 12,469
  • 5
  • 25
  • 52
  • When you have appended it, why is the comma missing between ">" and "1234". Aren't we supposed to see it like ">","1234" which is like 2 items/elements under each index. In that case, I can access them using index positions like [1][2] for example – The Great Mar 15 '19 at 05:10
  • I was just following your logic. `?append` should provide more details. As far as I know append adds something like a new "column" if this were a `data.frame` object. We could extract like: `purrr::map2(Operators,Numerics,function(x,y) append(x,y))[1][[1]][1]` – NelsonGon Mar 15 '19 at 05:16
  • can help me with this? https://stackoverflow.com/questions/63370682/unable-to-use-select-map-and-what-does-indicate-in-r – The Great Aug 12 '20 at 06:24
1

We could use a switch statement:

for (ch in operator){
  switch(ch, 
         ">"={
           #print("hello")
           operator_concept_id = append(operator_concept_id, 4172704L)
           value_as_concept_id = append(value_as_concept_id, 45876384L)   
         },
         "<"={
           #print("less")
           operator_concept_id = append(operator_concept_id, 4171756L)
           value_as_concept_id = append(value_as_concept_id, 45881666L)
         },
         "-"={
           #print("negative")
           operator_concept_id = append(operator_concept_id, 4172703L)
           value_as_concept_id = append(value_as_concept_id, 45878583L) 
         },
         {
           #print("hi")
           operator_concept_id = append(operator_concept_id, 4172703L)
           value_as_concept_id = append(value_as_concept_id, 45884084L)
         }
  )

}

Note that we cannot switch on "", instead, I used that as the default option at the end, so anything not fitting the previous cases will execute as that option.

JJJ
  • 969
  • 6
  • 19
  • 30