【问题标题】:How to select a part of a word within a list in R如何在R中的列表中选择单词的一部分
【发布时间】:2020-01-25 20:51:00
【问题描述】:

我有一个列表,其中包含按某些标准排序的最佳模型。我想获取变量的名称。例如,对于索引 [1,1],我得到以下信息:

print(opt_dos_variables_2[1,1])
c...Intercept.....ICI....LC.. 
                        "ICI"  

I just want the string between " ", i.e., the ICI.

What should I modify?

Thanks!

【问题讨论】:

标签: r string list


【解决方案1】:

您可以使用sub 作为基本 R 选项:

input <- "c...Intercept.....ICI....LC.. 
                    \"ICI\""
output <- sub("^.*\"(.*?)\".*$", "\\1", input)
output

[1] "ICI"

如果你想对整个列表使用这个逻辑,你可以使用lapply 和上面对sub 的调用作为内联函数,例如

lapply(your_list, function(x) sub("^.*\"(.*?)\".*$", "\\1", x))

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2020-05-21
    • 1970-01-01
    • 2017-05-23
    相关资源
    最近更新 更多