- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含 200 多个变量的数据框,其中许多变量以给定物种的代码结尾。我想消除包含多个代码之一的任何列,这些代码包含在单独的字符串向量中。如何同时删除与多个代码匹配的这些多列?列名与代码值不完全匹配,但包含列名末尾的代码。例如:
"rev230" "rev3360" "rev3508"
我已经手动完成了这个(使用 dplyr
包):
sub = select(df, -contains("3781"), -contains("3751"), -contains("1408"),
-contains("1409"), -contains("4469"), -contains("1789"), -contains("4559"),
-contains("1453"),-contains("8"), -contains("3508"), -contains("4656"),
-contains("5131"), -contains("9999"))
这让我得到了我想要的东西(消除了所有包含与这些代码匹配的物种数据的列),但显然这是非常乏味的。我想要更像的东西:
sub = select(df, -contains(species$codes))
# I realize this isn't the right syntax
我尝试了一个循环来删除单个列,使用类似这样的方法:
foreach(i=1:length(species$codes), .combine=rbind)%do%
select(df, -contains(species$codes[i]))
但这也没有用。提前致谢!
可重现的例子:物种代码(包含在字符向量中):
dput(species)
c("3754", "3755", "3758", "3764", "3765", "3771", "3772", "3782",
"3761", "3762", "3763", "3767", "3768", "1790", "1412", "1413",
"1416", "1422", "1423", "1424", "1425", "1426", "1410", "1411",
"1414", "1415", "1420", "3770", "4740", "4470", "4472", "4474",
"4476", "4479", "4480", "1812", "1815", "1799", "4560", "3810",
"1440", "1441", "3302", "3295", "3560", "3360", "1940", "3840",
"570", "1050", "4710", "230")
这是数据的前 10 行,只有物种数据列
dput(logsub)
structure(list(lbs3781 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 708), lbs3764 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3765 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 708), lbs3758 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3755 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3782 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), lbs3751 = c(0, 0, 4, 0, 0, 0, 21, 0, 18, 0), lbs3761 = c(0,
0, 0, 0, 0, 0, 18, 0, 0, 0), lbs3762 = c(0, 0, 4, 0, 0, 0, 3,
0, 0, 0), lbs3763 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3767 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3768 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), lbs3754 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3771 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3772 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), lbs1790 = c(0, 0, 0, 0, 0, 0, 0, 0, 18, 0), lbs1409 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 86), lbs1411 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), lbs1414 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lbs1415 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 86), lbs4740 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), lbs1420 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3770 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), lbs1408 = c(2508, 785, 57, 0, 132,
5003, 18, 104, 636, 0), lbs1412 = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0), lbs1413 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lbs1416 = c(2331,
654, 57, 0, 81, 4284, 15, 104, 120, 0), lbs1422 = c(177, 0, 0,
0, 51, 719, 3, 0, 0, 0), lbs1423 = c(0, 131, 0, 0, 0, 0, 0, 0,
502, 0), lbs1424 = c(0, 0, 0, 0, 0, 0, 0, 0, 14, 0), lbs1425 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), lbs1426 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), lbs1410 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lbs4469 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), lbs4470 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), lbs4472 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
lbs4474 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lbs4476 = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), lbs4479 = c(0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), lbs4480 = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0), lbs1789 = c(0, 0, 0, 863, 0, 0, 0, 0,
0, 98), lbs1812 = c(0, 0, 0, 863, 0, 0, 0, 0, 0, 27), lbs1815 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 71), lbs1799 = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0), lbs4559 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 12),
lbs4560 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 12), lbs3810 = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), lbs1453 = c(0, 0, 5,
0, 0, 0, 21, 0, 15, 235), lbs1440 = c(0, 0, 5, 0, 0, 0, 21,
0, 15, 0), lbs1441 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3560 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3302 = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 235), lbs3295 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
lbs0008 = c(0, 97, 99, 0, 0, 0, 0, 0, 0, 0), lbs1940 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3840 = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0), lbs1050 = c(0, 0, 31, 0, 0, 0, 0, 0, 0, 0),
lbs4710 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lbs570 = c(0,
97, 68, 0, 0, 0, 0, 0, 0, 0), lbs230 = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0), lbs3360 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), lbs3508 = c(0,
0, 5043, 0, 0, 0, 0, 0, 0, 0), lbs4656 = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0), lbs9999 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
rev3781 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1688.144979), rev3764 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), rev3765 = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 1688.144979), rev3758 = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0), rev3755 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev3782 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), rev3751 = c(0, 0, 7.063636364,
0, 0, 0, 33.44605263, 0, 32.53608247, 0), rev3761 = c(0,
0, 0, 0, 0, 0, 27.34105263, 0, 0, 0), rev3762 = c(0, 0, 7.063636364,
0, 0, 0, 6.105, 0, 0, 0), rev3763 = c(0, 0, 0, 0, 0, 0, 0,
0, 0, 0), rev3767 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev3768 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), rev3754 = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0), rev3771 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev3772 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), rev1790 = c(0, 0, 0, 0, 0, 0,
0, 0, 32.53608247, 0), rev1409 = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 260.0068669), rev1411 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), rev1414 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev1415 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 260.0068669), rev4740 = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0), rev1420 = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0), rev3770 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev1408 = c(6349.327025,
2014.2837, 142.8362084, 0, 339.5618788, 13265.98305, 41.94345809,
235.6862428, 1835.487932, 0), rev1412 = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0), rev1413 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
rev1416 = c(5841.249152, 1623.155767, 142.8362084, 0, 194.2835976,
11101.38378, 33.99320809, 235.6862428, 299.2968186, 0), rev1422 = c(508.0778723,
0, 0, 0, 145.2782813, 2164.599274, 7.95025, 0, 0, 0), rev1423 = c(0,
391.1279328, 0, 0, 0, 0, 0, 0, 1494.676782, 0), rev1424 = c(0,
0, 0, 0, 0, 0, 0, 0, 41.51433134, 0), rev1425 = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0), rev1426 = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0), rev1410 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev4469 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), rev4470 = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0), rev4472 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev4474 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), rev4476 = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0), rev4479 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L), rev4480 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev1789 = c(0,
0, 0, 963.8520574, 0, 0, 0, 0, 0, 95.34540063), rev1812 = c(0,
0, 0, 963.8520574, 0, 0, 0, 0, 0, 30.02711217), rev1815 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 65.31828847), rev1799 = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0), rev4559 = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 12.94965112), rev4560 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 12.94965112
), rev3810 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev1453 = c(0,
0, 3.505617978, 0, 0, 0, 13.9460241, 0, 10.93726937, 225.778089
), rev1440 = c(0, 0, 3.505617978, 0, 0, 0, 13.9460241, 0,
10.93726937, 0), rev1441 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), rev3560 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev3302 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 225.778089), rev3295 = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0), rev0008 = c(0, 180.3441341, 169.7750491,
0, 0, 0, 0, 0, 0, 0), rev1940 = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0), rev3840 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev1050 = c(0,
0, 48.71428571, 0, 0, 0, 0, 0, 0, 0), rev4710 = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0), rev570 = c(0, 180.3441341, 121.0607634,
0, 0, 0, 0, 0, 0, 0), rev230 = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0), rev3360 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), rev3508 = c(0,
0, 2620.957866, 0, 0, 0, 0, 0, 0, 0), rev4656 = c(0, 0, 0,
0, 0, 0, 0, 0, 0, 0), rev9999 = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0)), .Names = c("lbs3781", "lbs3764", "lbs3765", "lbs3758",
"lbs3755", "lbs3782", "lbs3751", "lbs3761", "lbs3762", "lbs3763",
"lbs3767", "lbs3768", "lbs3754", "lbs3771", "lbs3772", "lbs1790",
"lbs1409", "lbs1411", "lbs1414", "lbs1415", "lbs4740", "lbs1420",
"lbs3770", "lbs1408", "lbs1412", "lbs1413", "lbs1416", "lbs1422",
"lbs1423", "lbs1424", "lbs1425", "lbs1426", "lbs1410", "lbs4469",
"lbs4470", "lbs4472", "lbs4474", "lbs4476", "lbs4479", "lbs4480",
"lbs1789", "lbs1812", "lbs1815", "lbs1799", "lbs4559", "lbs4560",
"lbs3810", "lbs1453", "lbs1440", "lbs1441", "lbs3560", "lbs3302",
"lbs3295", "lbs0008", "lbs1940", "lbs3840", "lbs1050", "lbs4710",
"lbs570", "lbs230", "lbs3360", "lbs3508", "lbs4656", "lbs9999",
"rev3781", "rev3764", "rev3765", "rev3758", "rev3755", "rev3782",
"rev3751", "rev3761", "rev3762", "rev3763", "rev3767", "rev3768",
"rev3754", "rev3771", "rev3772", "rev1790", "rev1409", "rev1411",
"rev1414", "rev1415", "rev4740", "rev1420", "rev3770", "rev1408",
"rev1412", "rev1413", "rev1416", "rev1422", "rev1423", "rev1424",
"rev1425", "rev1426", "rev1410", "rev4469", "rev4470", "rev4472",
"rev4474", "rev4476", "rev4479", "rev4480", "rev1789", "rev1812",
"rev1815", "rev1799", "rev4559", "rev4560", "rev3810", "rev1453",
"rev1440", "rev1441", "rev3560", "rev3302", "rev3295", "rev0008",
"rev1940", "rev3840", "rev1050", "rev4710", "rev570", "rev230",
"rev3360", "rev3508", "rev4656", "rev9999"), row.names = c(34367L,
48646L, 48715L, 48717L, 48722L, 48724L, 48743L, 48744L, 48781L,
48783L), class = "data.frame")
最佳答案
一个选项是 select_if
和 grepl
,因为 grepl
返回一个逻辑向量,你可以通过 |
。
您想要移除两个物种的简单情况如下所示:
select_if(df, !grepl("3781|3751", names(df)) )
通过 paste
和 collapse
将你所有的物种聚合到 grepl
的正确形式,然后在 grepl
.
select_if(df, !grepl(paste(species, collapse = "|"), names(df)) )
关于从多个字符串中删除多个匹配列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39858468/
如何使用 SPListCollection.Add(String, String, String, String, Int32, String, SPListTemplate.QuickLaunchO
我刚刚开始使用 C++ 并且对 C# 有一些经验,所以我有一些一般的编程经验。然而,似乎我马上就被击落了。我试过在谷歌上寻找,以免浪费任何人的时间,但没有结果。 int main(int argc,
这个问题已经有答案了: In Java 8 how do I transform a Map to another Map using a lambda? (8 个回答) Convert a Map>
我正在使用 node + typescript 和集成的 swagger 进行 API 调用。我 Swagger 提出以下要求 http://localhost:3033/employees/sear
我是 C++ 容器模板的新手。我收集了一些记录。每条记录都有一个唯一的名称,以及一个字段/值对列表。将按名称访问记录。字段/值对的顺序很重要。因此我设计如下: typedef string
我需要这两种方法,但j2me没有,我找到了一个replaceall();但这是 replaceall(string,string,string); 第二个方法是SringBuffer但在j2me中它没
If string is an alias of String in the .net framework为什么会发生这种情况,我应该如何解释它: type JustAString = string
我有两个列表(或字符串):一个大,另一个小。 我想检查较大的(A)是否包含小的(B)。 我的期望如下: 案例 1. B 是 A 的子集 A = [1,2,3] B = [1,2] contains(A
我有一个似乎无法解决的小问题。 这里...我有一个像这样创建的输入... var input = $(''); 如果我这样做......一切都很好 $(this).append(input); 如果我
我有以下代码片段 string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.No
这可能真的很简单,但我已经坚持了一段时间了。 我正在尝试输出一个字符串,然后输出一个带有两位小数的 double ,后跟另一个字符串,这是我的代码。 System.out.printf("成本:%.2
以下是 Cloud Firestore 列表查询中的示例之一 citiesRef.where("state", ">=", "CA").where("state", "= 字符串,我们在Stack O
我正在尝试检查一个字符串是否包含在另一个字符串中。后面的代码非常简单。我怎样才能在 jquery 中做到这一点? function deleteRow(locName, locID) { if
这个问题在这里已经有了答案: How to implement big int in C++ (14 个答案) 关闭 9 年前。 我有 2 个字符串,都只包含数字。这些数字大于 uint64_t 的
我有一个带有自定义转换器的 Dozer 映射: com.xyz.Customer com.xyz.CustomerDAO customerName
这个问题在这里已经有了答案: How do I compare strings in Java? (23 个回答) 关闭 6 年前。 我想了解字符串池的工作原理以及一个字符串等于另一个字符串的规则是
我已阅读 this问题和其他一些问题。但它们与我的问题有些无关 对于 UILabel 如果你不指定 ? 或 ! 你会得到这样的错误: @IBOutlet property has non-option
这两种方法中哪一种在理论上更快,为什么? (指向字符串的指针必须是常量。) destination[count] 和 *destination++ 之间的确切区别是什么? destination[co
This question already has answers here: Closed 11 years ago. Possible Duplicates: Is String.Format a
我有一个Stream一个文件的,现在我想将相同的单词组合成 Map这很重要,这个词在 Stream 中出现的频率. 我知道我必须使用 collect(Collectors.groupingBy(..)
我是一名优秀的程序员,十分优秀!