[英]Make vector of rows that match a condition in R
I have a dataframe 'likes' that looks like this:我有一个像这样的数据框“喜欢”:
uid Likes
123 Harry Potter
123 Fitness
123 Muesli
123 Fanta
123 Nokia
321 Harry Potter
321 Muesli
455 Harry Potter
455 Muesli
699 Muesli
123 Belgium
Furthermore I have a bunch of strings, for example: WhatLikes <- c("Harry Potter","Muesli")
此外,我有一堆字符串,例如: WhatLikes <- c("Harry Potter","Muesli")
I want a vector of the uid's that 'like' Harry Potter OR Muesli.我想要一个“喜欢”哈利波特或麦片的 uid 向量。 Take note that WhatLikes
is much bigger than this example.请注意, WhatLikes
比此示例大得多。
The solution should thus be a vector that contains 123
, 321
, 455
, 699
.因此,解决方案应该是一个包含123
、 321
、 455
、 699
的向量。
Help me out!帮帮我! thanks!谢谢!
We can use %in%
to get a logical index of elements in 'Likes' that are found in 'WhatLikes'.我们可以使用%in%
来获取在“WhatLikes”中找到的“Likes”中元素的逻辑索引。 Get the corresponding 'uid' from the dataset.从数据集中获取相应的“uid”。 Apply unique
to remove the duplicate 'uid's.应用unique
删除重复的 'uid'。
unique(df1$uid[df1$Likes %in% WhatLikes])
#[1] 123 321 455 699
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.