【问题标题】:R transform character vectors in a list, conditional on the content of the vectorR 变换列表中的字符向量,以向量的内容为条件
【发布时间】:2022-08-19 01:48:11
【问题描述】:

问题是我有一个字符向量列表。

例子:

mylist <- list( c(\"once\",\"upon\",\"a\",\"time\"),
                c(\"once\", \"in\", \"olden\", \"times\"),
                c(\"Let\",\"all\",\"good\",\"men\"),
                c(\"Let\",\"This\"),
                c(\"once\", \"is\",\"never\",\"enough\"),
                c(\"in\",\"the\"),
                c(\"Come\",\"dance\",\"all\",\"around\"))

我想将 c(\"one\", \"two\") 添加到那些以 \"once\" 开头的向量以结束列表

mylist <- list( c(\"one\", \"two\", \"once\",\"upon\",\"a\",\"time\"),
                c(\"one\", \"two\", \"once\", \"in\", \"olden\", \"times\"),
                c(\"Let\",\"all\",\"good\",\"men\"),
                c(\"Let\",\"This\"),
                c(\"one\", \"two\", \"once\", \"is\",\"never\",\"enough\"),
                c(\"in\",\"the\"),
                c(\"Come\",\"dance\",\"all\",\"around\"))

至今

我可以选择相关的向量

mylist[grep(\"once\",mylist)]

我可以在前面加上 \"one\" 和 \"two\" 来创建一个结果列表

resultlist <- lapply(mylist[grep(\"once\",mylist)],FUN = function(listrow) prepend(listrow,c(\"One\",\"Two\")))

但是将结果放在 mylist 中的正确位置?

不,这逃脱了我!

提示,提示和解决方案最受欢迎:-)

    标签: r list conditional-statements character transform


    【解决方案1】:
    • 我们可以使用
    lapply(mylist , \(x) if(grepl("once" , x[1])) 
          append(x,  c("one", "two") , 0) else x)
    
    • 输出
    [[1]]
    [1] "one"  "two"  "once" "upon" "a"    "time"
    
    [[2]]
    [1] "one"   "two"   "once"  "in"    "olden" "times"
    
    [[3]]
    [1] "Let"  "all"  "good" "men" 
    
    [[4]]
    [1] "Let"  "This"
    
    [[5]]
    [1] "one"    "two"    "once"   "is"     "never"  "enough"
    
    [[6]]
    [1] "in"  "the"
    
    [[7]]
    [1] "Come"   "dance"  "all"    "around"
    

    【讨论】:

      【解决方案2】:

      我认为你根本不需要grep。遍历列表,检查"once" 的第一个值并通过c() 附加额外的值:

      lapply(mylist, \(x) if(x[1] == "once") c("one", "two", x) else x)
      ##[[1]]
      ##[1] "one"  "two"  "once" "upon" "a"    "time"
      ##
      ##[[2]]
      ##[1] "one"   "two"   "once"  "in"    "olden" "times"
      ##
      ##[[3]]
      ##[1] "Let"  "all"  "good" "men" 
      ##
      ##[[4]]
      ##[1] "Let"  "This"
      ##
      ##[[5]]
      ##[1] "one"    "two"    "once"   "is"     "never"  "enough"
      ##
      ##[[6]]
      ##[1] "in"  "the"
      ##
      ##[[7]]
      ##[1] "Come"   "dance"  "all"    "around"
      

      【讨论】:

      • 完美运行。谢谢你。只是为了我的启迪,\(x)是什么意思?
      • \(x)function(x) 的简写,其中xmylist 的每个元素的占位符,因为它被lapply 循环。
      【解决方案3】:

      map_if 的另一个选择

      library(purrr)
      map_if(mylist, .p = ~ first(.x) == "once", .f = ~ c("one", "two", .x))
      

      -输出

      [[1]]
      [1] "one"  "two"  "once" "upon" "a"    "time"
      
      [[2]]
      [1] "one"   "two"   "once"  "in"    "olden" "times"
      
      [[3]]
      [1] "Let"  "all"  "good" "men" 
      
      [[4]]
      [1] "Let"  "This"
      
      [[5]]
      [1] "one"    "two"    "once"   "is"     "never"  "enough"
      
      [[6]]
      [1] "in"  "the"
      
      [[7]]
      [1] "Come"   "dance"  "all"    "around"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-09
        • 2018-10-14
        • 1970-01-01
        • 2015-08-23
        • 1970-01-01
        • 1970-01-01
        • 2017-02-19
        • 2020-01-24
        相关资源
        最近更新 更多