如何在r中使用类型列表()的列上的条件过滤数据表行,r
我有一个data.table
,看起来像 (仅显示许多列中的几列) -
ID | 期间 | 产品 |
---|---|---|
1000797366 | 2018-q1 | UG10000000000000000-WISD |
1000797366 | 2018-Q1 | NX11100100 ,UG10000-WISD,UG12210 |
1000797366 | 2018-Q1 | UG10000-WISD,UG12210 |
1000797366 | 2018-Q1 | UG10000-WISD,UG12210 |
1000797366 | 2018-Q1 | UG12210 |
1000797366 | 2018-Q1 | NX11100 |
1000797366 | 2018-Q1 2018-Q1 | NX11100 NX11100 |
在此处“产品”列的“产品”,这样,以便以后的用法。
但是我在根据产品列上的条件过滤行时面临问题。
我想要的是过滤所有行,而其他文件的值可以是该向量C(“ UG12210”,“ UG10000-WISD”)的所有行 例如C中的周期(“ 2018-Q1”)。
因此,我的输出应该看起来像这样
-ID | 期 | 产品 |
---|---|---|
1000797366 | 2018-Q1 | UG10000-WISD |
1000797366 | 2018-Q1 | NX11100,UG10000-WISD,UG122210 |
1000797366 | 2018 -Q1 | Wis121111211012111118 |
UG1000000000000000000000000000000000000000000-21000000个 | -Q111118 -Q1 | 20186666110 YUG12110姐妹 - |
1000797366 | 2018-Q1 | UG12210 |
,但这并没有以某种方式发生,我尝试了以下条件,但没有奏效。
data_test[Period %in% c("2018-Q1") & is.element("UG12210",Product),]
data_test[Period %in% c("2018-Q1") & Product %in% c("UG12210"),]
关于如何实现它的任何潜在客户都会有很大的帮助。谢谢!
下面是数据使用 dput()的数据
structure(
list(
Id = c("1000797366", "1000797366", "1000797366", "1000797366", "1000797366", "1000797366", "1000797366"),
Period = c("2018-Q1", "2018-Q1", "2018-Q1", "2018-Q1", "2018-Q1", "2018-Q1", "2018-Q1"),
Product = list("UG10000-WISD", c("NX11100", "UG10000-WISD", "UG12210"), c("UG10000-WISD", "UG12210"),
c("UG10000-WISD", "UG12210"), "UG12210", "NX11100", "NX11100")
),
row.names = c(NA,-7L),
class = c("data.table", "data.frame"),
.internal.selfref = < pointer:0x562f66275020 >
)
I have a data.table
which looks something like this (showing just few columns out of many) -
Id | Period | Product |
---|---|---|
1000797366 | 2018-Q1 | UG10000-WISD |
1000797366 | 2018-Q1 | NX11100, UG10000-WISD, UG12210 |
1000797366 | 2018-Q1 | UG10000-WISD, UG12210 |
1000797366 | 2018-Q1 | UG10000-WISD, UG12210 |
1000797366 | 2018-Q1 | UG12210 |
1000797366 | 2018-Q1 | NX11100 |
1000797366 | 2018-Q1 | NX11100 |
Here the column "Product" is of type list()
as I've to keep it this way for some later usage.
But I am facing a problem while filtering the rows based on a condition on Product column.
What I want is to filter all rows where value of product can be any of this vector c("UG12210","UG10000-WISD") along-with other filer
such as Period in c("2018-Q1").
So my output should look something like this -
Id | Period | Product |
---|---|---|
1000797366 | 2018-Q1 | UG10000-WISD |
1000797366 | 2018-Q1 | NX11100, UG10000-WISD, UG12210 |
1000797366 | 2018-Q1 | UG10000-WISD, UG12210 |
1000797366 | 2018-Q1 | UG10000-WISD, UG12210 |
1000797366 | 2018-Q1 | UG12210 |
But somehow this is not happening, I tried following conditions but none worked.
data_test[Period %in% c("2018-Q1") & is.element("UG12210",Product),]
data_test[Period %in% c("2018-Q1") & Product %in% c("UG12210"),]
Any leads on how it can be achieved will be of great help. Thanks!
Below is the data using dput() for the datatable
structure(
list(
Id = c("1000797366", "1000797366", "1000797366", "1000797366", "1000797366", "1000797366", "1000797366"),
Period = c("2018-Q1", "2018-Q1", "2018-Q1", "2018-Q1", "2018-Q1", "2018-Q1", "2018-Q1"),
Product = list("UG10000-WISD", c("NX11100", "UG10000-WISD", "UG12210"), c("UG10000-WISD", "UG12210"),
c("UG10000-WISD", "UG12210"), "UG12210", "NX11100", "NX11100")
),
row.names = c(NA,-7L),
class = c("data.table", "data.frame"),
.internal.selfref = < pointer:0x562f66275020 >
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
sapply
函数来检查vals
中的任何值是否在product
中:You can use
sapply
function to check if any of the values invals
is inProduct
for each row:我们可以使用
lapply 在
list
上循环,检查是否有是否有
值) )在每个列表元素和子集vector> vector
('v1'的任何输出中
We can loop over the
list
withlapply
, check if there areany
values from thevector
('v1') in each of the list elements and subset-output