R 将数字复杂赋值给字母的函数

R 将数字复杂赋值给字母的函数,r,string,function,dplyr,R,String,Function,Dplyr,原始问题:我正在努力编写一个函数,用于在复杂条件下为字母分配数字。请看一看tibble示例。我需要一种方法来获取df$nr中的值 library(tidyverse) df <-tibble(word=c(rep("Hobbies", 17), rep("tate", 11)), let=c("H1","H1","H2","H2","o","o","b","b","b","b","i","i","e","e","iP","iP","s",

原始问题:我正在努力编写一个函数,用于在复杂条件下为字母分配数字。请看一看tibble示例。我需要一种方法来获取df$nr中的值

library(tidyverse)
df <-tibble(word=c(rep("Hobbies", 17), rep("tate", 11)),
            let=c("H1","H1","H2","H2","o","o","b","b","b","b","i","i","e","e","iP","iP","s",
                  "t1","t1","a","a","t1","t1","e","e","t2","t2","t2"),
            tra=c(NA,"H2",NA,"o",NA,"b",NA,"b",NA,"i",NA,"e",NA,"iP",NA,"s",NA,
                  NA,"a",NA,"t1",NA,"e",NA,"t2",NA,"t2",NA),
            nr= c(1,1,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6,6,6.5,7,
                  1,1.5,2,2.5,3,3.5,4,4,4,4,4) )

这看起来像是一种情况,
dplyr::case\u when
会有所帮助。但是你有很多讨厌的正则表达式要处理,stringr包可能会有所帮助。@markhogue谢谢,到时候会看一看
case_
。你能提供一个更高层次的解释,说明你试图用伪代码实现什么吗?@Roger-123自己找到了一个解决方案并发表了评论。仍有错误,但结果正常。
df <- df %>%  
  group_by(word) %>%  
  mutate(  
    test = cumsum(  
      case_when (  
        # first letter per word -> 1  
        row_number()==1L                            ~ 1,  
        # tra is NA -> full letter, should result in an integer in the end  
        # special case: dots (e.g. on 'i' -> "iP", this is not a full letter -> do nothing  
        is.na(tra) & str_sub(let,2,2)=="P"          ~ 0,  
        # special case: fraction of a letter (e.g. "H2" or "t2"), i.e. number is > 1 -> do nothing  
        is.na(tra) & as.integer(str_sub(let,2,2))>1 ~ 0,  
        # full letter -> 0.5  
        is.na(tra)                                  ~ 0.5,  
        # tra is present -> trajectory towards next letter, should result in .5 in the end  
        # special case: tra to dot -> do nothing  
        str_sub(tra,2,2)=="P"                       ~ 0,  
        # special case: tra to fraction -> do nothing  
        as.integer(str_sub(tra,2,2))>1              ~ 0,  
        # all other tra  
        TRUE                                        ~ 0.5  
        )  
      )  
    )  

all.equal(df$nr, df$test)  
[1] TRUE