R Merge multiple vectors alternatively [duplicate] - r

This question already has an answer here:
Combine two equal length vectors alternating [duplicate]
(1 answer)
Closed 6 years ago.
I have 3 vectors
x<- 1:3
y<- 4:6
z<- 7:9
I want to do combine these three vectors into single vector k such that
k
[1] 1,4,7,2,5,8,3,6,9
I did this
k<-c()
for(i in 1:length(x)){
l<-c(x[i],y[i],z[i])
k<-c(k,l)
}
I want to avoid loops. Does anyone know how to do this without using a loop?
Thanks

We can rbind the vectors to a matrix and convert it to a vector with c
c(rbind(x,y,z))
#[1] 1 4 7 2 5 8 3 6 9

Related

R - values of x between 3 and 7 [duplicate]

This question already has answers here:
Check to see if a value is within a range?
(7 answers)
Closed 5 years ago.
I'm starting to learn R, as it's needed for work. I have never done statistical work, so I'm a bit lost.
I'm looking to get the value of x between two numbers.
So, for example, the range is 3:7 I need to print 4,5,6
I have tried
x <- 3:7
x[x>3 && x<7]
and
x <- 3
v <- 7
cbind(x, findInterval(x, v))
Any advice/guidelines
An option is between from data.table
x[data.table::between(x, 3, 7, incbounds = FALSE)]
#[1] 4 5 6

R: Extracting non-duplicated values from vector (not keeping one value for duplicates) [duplicate]

This question already has answers here:
Finding ALL duplicate rows, including "elements with smaller subscripts"
(9 answers)
How can I remove all duplicates so that NONE are left in a data frame?
(3 answers)
Closed 5 years ago.
I would like to keep the non-duplicated values from a vector, but without retaining one element from duplicated values. unique() does not work for this. Neither would duplicated().
For example:
> test <- c(1,1,2,3,4,4,4,5,6,6,7,8,9,9)
> unique(test)
[1] 1 2 3 4 5 6 7 8 9
Whereas I would like the result to be: 2,3,5,7,8
Any ideas on how to approach this? Thank you!
We can use duplicated
test[!(duplicated(test)|duplicated(test, fromLast=TRUE))]
#[1] 2 3 5 7 8
You can use ave to count the length of sub-groups divided by unique values in test and retain only the ones whose length is 1 (the ones that have no duplicates)
test[ave(test, test, FUN = length) == 1]
#[1] 2 3 5 7 8
If test is comprised of characters, use seq_along as first argument of ave
test[ave(seq_along(test), test, FUN = length) == 1]

Mean(or other function) of corresponding elements of a list in R [duplicate]

This question already has answers here:
How to sum a numeric list elements
(2 answers)
Closed 6 years ago.
I have a list, each element in this list is a vector and have same length. I want to calculate the mean(or other value, it can be a user-defined function) of all first element of each vector, mean(or other value, it can be a user-defined function) of all second element of each vector, etc. And return a vector. So this is different from question How to sum a numeric list elements in R .Following code gave me the exactly what I want, however, is there any more efficient and sophistical way to do this? Thanks.
list1 <- list(a=1:5,b=2:6,c=3:7)
result <- numeric(length(list1[[1]]))
for(i in 1:length(list1[[1]])){
result[i] <- mean(c(list1[[1]][i],list1[[2]][i],list1[[3]][i])) #the function can be any other function rather than mean()
}
Here's an option using the Reduce function:
Reduce("+",list1)/length(list1)
[1] 2 3 4 5 6
How about putting them all in a matrix and then calculating the means of the columns?
colMeans(do.call(rbind, list1))
[1] 2 3 4 5 6

Append dataframe to start of a list [duplicate]

This question already has answers here:
Appending a list to a list of lists in R
(7 answers)
Closed 6 years ago.
I have 1 list A containing 2 vectors and 1 vector B. I want to insert B into A such that B is in the first position.
I have this:
A <- list(LETTERS, letters)
B <- 1:10
I want this:
A <- list(B, A[[1]], A[[2]])
I currently do this:
B <- list(B)
for (i in 1:length(A)) {
B[i+1] <- A[[i]]}
Is there a better way without using for loops?
You can simply use c :
c(list(B), A)

How to concatenate c(1,2,3) and c(4,5,6) to c(1,4,2,5,3,6) in R? [duplicate]

This question already has answers here:
Alternate, interweave or interlace two vectors
(2 answers)
Closed 8 years ago.
How can I concatenate two vectors to get a vector with values alternatively from the first and second one?
Input
a<- c(1,2,3)
b<- c(4,5,6)
Output
c(1,4,2,5,3,6)
This is one way
> as.numeric(t(matrix(c(a,b), ncol = 2)))
[1] 1 4 2 5 3 6

Resources