r - 组合列表的所有元素并仅保留唯一值

标签 r list

我有一个包含几列的数据框SCC。我想从这些列中找到所有包含“煤炭”一词的行。

我使用 grep 函数来执行此操作,并使用以下命令将结果存储在变量 x (列表)中:

x <- sapply(SCC, grep, pattern="coal", ignore.case=T)

现在x告诉我数据框中单词“coal”出现的位置,它是一个列表,下面给出了该列表的第4个和第9个元素。

> x[4]
$EI.Sector
 [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
[17]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  80  81
[33]  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98
[49]  99 100 101 102 103 104 105 106 107 108 109 110 111 112 161 162
[65] 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
[81] 179 180 181 182 183 184 185 215 221 433 434 435 447 448 462 463
[97] 527 528 529

> x[9]
$SCC.Level.Three
  [1]    1    2    3    4    5    6    7    8    9   10   11   12   13
 [14]   14   15   16   17   18   19   20   21   79   80   81   83   84
 [27]   85   86   87   88   89   90   91   92   93   94   95   96   97
 [40]   98   99  100  101  102  103  104  105  161  162  163  164  165
 [53]  166  167  168  169  170  171  172  173  174  175  176  177  178
 [66]  179  180  242  433  434  435  447  448  462  463  477  478  527
 [79]  528  529 2220 2221 2222 2223 2224 2496 2497 2498 2499 2500 2501
 [92] 2502 2503 2504 2505 2506 2591 2592 4520 4673 4674 4675 4676 4677
[105] 4678 4679 4680 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424
[118] 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437
[131] 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450
[144] 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7991
[157] 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004
[170] 8005 8006 8007 8008 8009 9054 9055 9056 9057 9058 9059 9060

问题1

我想将 x[4]x[9] 合并到一个索引中;我应该如何进行?我现在正在使用下面的代码,但是有更简洁的命令吗?

y <- x[[4]] %in% intersect(x[[4]], x[[9]])
z <- c( x[[4]][!y], x[[9]])

> sort(z)
  [1]    1    2    3    4    5    6    7    8    9   10   11   12
 [13]   13   14   15   16   17   18   19   20   21   22   23   24
 [25]   25   26   27   28   29   30   79   80   81   83   84   85
 [37]   86   87   88   89   90   91   92   93   94   95   96   97
 [49]   98   99  100  101  102  103  104  105  106  107  108  109
 [61]  110  111  112  161  162  163  164  165  166  167  168  169
 [73]  170  171  172  173  174  175  176  177  178  179  180  181
 [85]  182  183  184  185  215  221  242  433  434  435  447  448
 [97]  462  463  477  478  527  528  529 2220 2221 2222 2223 2224
[109] 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2591
[121] 2592 4520 4673 4674 4675 4676 4677 4678 4679 4680 7415 7416
[133] 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428
[145] 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440
[157] 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452
[169] 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7991 7992
[181] 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004
[193] 8005 8006 8007 8008 8009 9054 9055 9056 9057 9058 9059 9060

问题2

SCC 中还有其他带有“煤炭”一词的列。 x 元素的长度让我们了解“coal”这个词在 SCC 中的使用位置。是否有一个命令可以组合 x 的所有元素来生成行索引?

sapply(x, NROW)
                SCC       Data.Category          Short.Name 
                  0                   0                 239 
          EI.Sector        Option.Group          Option.Set 
                 99                   0                   0 
      SCC.Level.One       SCC.Level.Two     SCC.Level.Three 
                  0                   0                 181 
     SCC.Level.Four              Map.To Last.Inventory.Year 
                126                   0                   0 
       Created_Date        Revised_Date         Usage.Notes 
                  0                   0                   0 

最佳答案

(将评论变成答案)

你不是在做z <- union(x[[4]], x[[9]])吗? ?

union does the job, is there a way to do union on all the element of x simultaneously?

union() takes only two vectors; is there another function that would take as argument all the elements of x?

我们可以简单地做

unique(unlist(x))

如果您想继续使用union ,做:

Reduce(union, x)

关于r - 组合列表的所有元素并仅保留唯一值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41403930/

相关文章:

python - 在 R 中读取 Feather 对象很慢

python - 如何使用 python 将列表中的信息填充到多个 JSON 对象中?

r - 如何在不使用 print() 的情况下打印函数的结果?

R:错误应用于类 "c(' 整数', 'numeric' ) 的对象”

python - 根据组号制作新列表并添加分数

java - 如何使用二进制搜索从已排序的 TreeSet 中检索元素?

python - 通过乘法创建列表

c# - 在 C# 中,如何将 List<int> 序列化为 byte[] 以便将其存储在数据库字段中?

根据列中的值返回数据帧的行 - R

r - 将日期时间列拆分为日期和时间变量