【问题标题】:I wish to split the names of lists and allocate them in order R我希望拆分列表的名称并按顺序分配它们 R
【发布时间】:2019-10-24 21:54:26
【问题描述】:

我有一个列表列表,其中一些列表的名称是重复的,即使列表中的信息本身并不相同。这是一个例子:

l.f[[" c225 && Gross Amount && Reporting Year of Account 2015 && 225 && Gross Amount && Reporting Year of Account 2016 && 225 && Gross Amount && Reporting Year of Account 2016 && 1"]]

l.f[[" c225 && Gross Amount && Reporting Year of Account 2015 && 225 && Gross Amount && Reporting Year of Account 2016 && 225 && Gross Amount && Reporting Year of Account 2016 && 2"]]

l.f[[" c225 && Gross Amount && Reporting Year of Account 2015 && 225 && Gross Amount && Reporting Year of Account 2016 && 225 && Gross Amount && Reporting Year of Account 2016 && 3"]]

在这种情况下,唯一区别这些列表名称的是每个列表名称中的最后一个数字(1,2,3)。我想做的是分解这些列表的名称,以便它们的命名如下:

l.f[[" c225 && Gross Amount && Reporting Year of Account 2015"]]

l.f[["225 && Gross Amount && Reporting Year of Account 2016"]]

l.f[["225 && Gross Amount && Reporting Year of Account 2016 1"]]

有没有办法在 R 中像这样分解它?

【问题讨论】:

  • 所有相似的字符串是否以相同的方式相似,只是末尾的数字不同?
  • @TimBiegeleisen 是的,唯一能区分它们的是末尾的数字,范围从 1 到 7,具体取决于列表名称

标签: r string list names


【解决方案1】:

一个选项是删除&& 之后的字符并使用make.unique

names(lst1) <- make.unique(sub("^([^&]+[&]+[^&]+[&]+[^&]+)\\s+.*", "\\1", names(lst1)))

数据

names(lst1)
c(" c225 && Gross Amount && Reporting Year of Account 2015 && 225 && Gross Amount && Reporting Year of Account 2016 && 225 && Gross Amount && Reporting Year of Account 2016 && 1", 
 " c225 && Gross Amount && Reporting Year of Account 2015 && 225 && Gross Amount && Reporting Year of Account 2016 && 225 && Gross Amount && Reporting Year of Account 2016 && 2", 
 " c225 && Gross Amount && Reporting Year of Account 2015 && 225 && Gross Amount && Reporting Year of Account 2016 && 225 && Gross Amount && Reporting Year of Account 2016 && 3"
 )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2015-03-31
    • 2018-04-03
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多