This question already has answers here:
Find all positions of all matches of one vector of values in second vector
(3 answers)
Finding All Positions for Multiple Elements in a Vector
(7 answers)
Closed 4 years ago.
In the following example, there is repeating entries in the second vector. How do I make R to tell me the position of both appearances of 5, please?
match(5, c(1,2,9,5,3,6,7,4,5))
Use the which function:
which(c(1,2,9,5,3,6,7,4,5) == 5)
Related
This question already has answers here:
string starting with specific pattern
(2 answers)
Filter a vector of strings based on string matching
(4 answers)
Closed 3 years ago.
I have a vector which have following three strings
test <- c("www.example.com/topic/university-admission",
"www.example.com/topic/school-admission",
"www.example.com/college-admission")
I want to extract all the element which have pattern like this "www.example.com/topic/"
One way is just looking at vector and extract as per the index location of that element.
But this will be challenging if vector has length of say 100.
Is there any way to do this using string pattern matching?
test[startsWith(x = test, "www.example.com/topic/")]
# [1] "www.example.com/topic/university-admission" "www.example.com/topic/school-admission"
This question already has answers here:
Extract every nth element of a vector
(5 answers)
How can I get mean of every n rows and keep the date index?
(7 answers)
Sum every nth points
(9 answers)
Closed 3 years ago.
Say I have a vector. I need to select every two elements. I need to do this in a loop and pass it on to different vector
for eg
A<-c(1:10)
i want to select elements like
(1,2) (3,4),(5,6) and so on and pass the sum of these two elements into another vector.
required output is (3,7,11,15)
This question already has answers here:
Getting the last n elements of a vector. Is there a better way than using the length() function?
(6 answers)
Closed 5 years ago.
I am working in a dataframe in R and I want to access the last 5 objects of a particular column in a dataframe.How do i go about it??
One option is tail(dataframe$column, 5)
This question already has answers here:
How to get all possible combinations of n number of data set?
(2 answers)
How to calculate combination and permutation in R?
(6 answers)
Closed 5 years ago.
I am getting a tough time in selecting the combinations of factors.
I have a vector as ("Ryan", "Leo", "Jack","Harry","Edd").
I want to get the list of of all combinations taken 3 of the names once.
I want to do it in R. Probably a resultant matrix will help me.
We can use combn
t(combn(vec1, 3))
This question already has answers here:
How to count the number of unique values by group? [duplicate]
(1 answer)
Unique values in each of the columns of a data frame
(9 answers)
Closed 5 years ago.
Is there better way to compute this instead of:
length(unique(vector))
let's assume that we donno what class is the vector.
You can use
library(data.table)
vector <- c(1,2,2,2,3)
uniqueN(vector)