Sub setting a block of elements inside a vector [duplicate] - r

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)

Related

Numbering a Vector that Has Repeats Up Until a New Element [duplicate]

This question already has answers here:
Increment by 1 for every change in column
(6 answers)
Increase counter upon change of value [duplicate]
(1 answer)
Count changes to contents of a character vector [duplicate]
(2 answers)
Closed 2 years ago.
If I have a vector such as :
c("A","A","B","B","A","C","C","C","D","A","B","C")
And I'm interested in numbering it, based upon repeats upon until they hit a new element. Such as:
1,1,2,2,3,4,4,4,5,6,7,8
How can I go about this? So far, I wrote this:
x <- map(seq_along(u), ~str_locate_all(str_flatten(u), u[[.x]]))

How to extract similar element from a vector? [duplicate]

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"

Repeating entries in match in R [duplicate]

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)

R- list of combination of 3 item sets from n items [duplicate]

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))

The most efficient way to calculate numbero of unique values in vector [duplicate]

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)

Resources