Find all positions of all matches of one vector of values in second vector
This should work:
which(hay %in% needles) # 2 3 5
Position of elements from one vector in another vector with R
You are looking for pmatch
:
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,3,4,5)
pmatch(b,a)
#[1] 1 3 5 7 9
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,5)
pmatch(b,a)
#[1] 1 3 4 5 7 9
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,4,5)
pmatch(b,a)
#[1] 1 3 4 5 7 8 9
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,2,2,3,4,4,5,5)
pmatch(b,a)
#[1] 1 3 4 5 7 8 9 10
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,1,2,2,3,4,4,5,5)
pmatch(b,a)
#[1] 1 2 3 4 5 7 8 9 10
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c(1,1,2,2,3,3,4,4,5,5)
pmatch(b,a)
# [1] 1 2 3 4 5 6 7 8 9 10
How to find indices of element in one vector in other vector R
a <- c('Q1', 'Q2', 'Q3')
b <- c('Q10', 'Q13', 'Q1', 'Q1', 'Q40', 'Q2', 'Q2', 'Q2')
which(b %in% a)
[1] 3 4 6 7 8
Get the index of the values of one vector in another?
Getting indexes of values is what match()
is for.
first = c( "a" , "c" , "b" )
second = c( "c" , "b" , "a" )
match(second, first)
[1] 2 3 1
Finding All Positions for Multiple Elements in a Vector
This is one way to do it. First I get the indices at which x is either 8 or 9. Then we can verify that at those indices, x is indeed 8 and 9.
> inds <- which(x %in% c(8,9))
> inds
[1] 1 3 4 12 15 19
> x[inds]
[1] 8 9 9 8 9 8
Find matches of a vector of strings in another vector of strings
You can try pasting your "keywords" together and separate them with the pipe character (|
) which will work like an "or", like this:
> articles[grepl(paste(keywords, collapse="|"), articles$text),]
id text
1 1 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
2 2 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
4 4 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
How to detect multiple strings in another vector of strings
You just need:
all_words %in% words
From help("%in%")
:
%in% is a more intuitive interface as a binary operator, which returns
a logical vector indicating if there is a match or not for its left
operand.
Basically for each element in the first vector, it checks if there's a match in the left hand vector.
Get indexes of a vector of numbers in another vector
Using base R you could do the following:
v <- c(2,2,3,5,8,0,32,1,3,12,5,2,3,5,8,33,1)
x <- c(2,3,5,8)
idx <- which(v == x[1])
idx[sapply(idx, function(i) all(v[i:(i+(length(x)-1))] == x))]
# [1] 2 12
This tells you that the exact sequence appears twice, starting at positions 2 and 12 of your vector v
.
It first checks the possible starting positions, i.e. where v
equals the first value of x
and then loops through these positions to check if the values after these positions also equal the other values of x
.
Get indices of matches with a column in a second data.table
Using .EACHI
and adding the resulting list column by reference.
dt2[ , res := dt1[ , i := .I][.SD, on = .(firstName), .(.(i)), by = .EACHI]$V1]
# lid firstName res
# 1: 1 Maria NA
# 2: 2 Jim 1,4
# 3: 3 Jack NA
# 4: 4 Anne 3,5
Related Topics
Why Is Subsetting on a "Logical" Type Slower Than Subsetting on "Numeric" Type
Generate All Possible Permutations (Or N-Tuples)
Number Format, Writing 1E-5 Instead of 0.00001
First Day of the Month from a Posixct Date Time Using Lubridate
Should I Avoid Programming Packages with Pipe Operators
How to Find Common Rows Between Two Dataframe in R
Using R to Download Zipped Data File, Extract, and Import .Csv
Convert Quarter/Year Format to a Date
Overlay Geom_Points() on Geom_Boxplot(Fill=Group)
Time Series Plot with X Axis in "Year"-"Month" in R
Legends for Multiple Fills in Ggplot
Add Text on Right of Shinydashboard Header
Update a Column of Nas in One Data Table with the Value from a Column in Another Data Table
R Subset with Condition Using %In% or ==. Which One Should Be Used
Group by and Conditionally Count
How to Create a Bar Plot for Two Variables Mirrored Across the X-Axis in R