Elegant way to write function

旧城冷巷雨未停 提交于 2019-12-11 18:27:41

问题


I have an input column (symbols) which has more than 10000 rows and they contain operator symbols and text values like ("",">","<","","****","inv","MOD","seen") as shown below in the code as values. This column doesn't contain any numbers. It only contains the value which are stated in the code.

What I would like to do is map those operator symbols ('<','>' etc) to different codes, 1) Operator_codes 2) Value_codes and have these two different codes as separate columns

I already have a working code but it is not very efficient as you can see I repeat the same operation twice. Once for Operator_codes and then for value_codes. I am sure there must be some efficient way to write this. I am new to R and not very familiar with other approach.

oper_val_concepts = function(DF){
operators_source = str_extract(.$symbols)
operators_source = as.data.frame(operators_source)
colnames(operators_source) <- c("Symbol")
operator_list = c("",">","<","-","****","inv","MOD","seen")
operator_codes = c(123L,14L,16L,13L,0L,0L,0L,0L)
value_codes=c(14L,12L,32L,123L,16L
,41L,116L,186L)
operator_code_map = map2(operator_list,operator_codes,function(x,y)c(x,y)) 
  %>% 
    data.frame()
value_code_map = map2(operator_list,value_codes,function(x,y) c(x,y)) %>% 
  data.frame()
operator_code_map = t(operator_code_map)
value_code_map = t(value_code_map)
colnames(operator_code_map) <- c("Symbol","Code")
colnames(value_code_map) <- c("Symbol","Code")
rownames(operator_code_map) = NULL
rownames(value_code_map) = NULL
dfm<-merge(x=operators_source,y=operator_code_map,by="Symbol",all.x = 
TRUE)
dfm1<-merge(x=operators_source,y=value_code_map,by="Symbol",all.x = TRUE)
 }

 t1 = oper_val_concepts(test)

dput command output is

structure(list(Symbols = structure(c(2L, 3L, 1L, 4L, 2L, 3L, 
5L, 4L, 6L), .Label = c("****", "<", ">", "inv", "mod", "seen"
), class = "factor")), .Names = "Symbols", row.names = c(NA,-9L), class = 
"data.frame")

I am expecting an output to be two columns in a dataframe as shown below.


回答1:


Based on what I am understanding, it seems like you want to create a dataframe that will act as a key (see key below). Once you have this, you can just join the dataframe that just contains symbols with this key dataframe.

df <- structure(list(Symbols = structure(c(2L, 3L, 1L, 4L, 2L, 3L, 
                                     5L, 4L, 6L), .Label = c("****", "<", ">", "inv", "mod", "seen"
                                     ), class = "factor")), .Names = "Symbols", row.names = c(NA, -9L), class = "data.frame")

key <- data.frame(Symbols = c("",">","<","-","****","inv","mod","seen"),
           Oerator_code_map = c(123L,14L,16L,13L,0L,0L,0L,0L),
           value_code_map = c(14L,12L,32L,123L,16L,41L,116L,186L))

df %>% left_join(key, by = "Symbols")

output

  Symbols Oerator_code_map value_code_map
1       <               16             32
2       >               14             12
3    ****                0             16
4     inv                0             41
5       <               16             32
6       >               14             12
7     mod                0            116
8     inv                0             41
9    seen                0            186


来源:https://stackoverflow.com/questions/55473181/elegant-way-to-write-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!