Append dataframe to start of a list [duplicate] - r

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)

Related

Convert each list element of different length into a column in R [duplicate]

This question already has answers here:
How to cbind or rbind different lengths vectors without repeating the elements of the shorter vectors?
(6 answers)
How to convert a list consisting of vector of different lengths to a usable data frame in R?
(6 answers)
Closed last month.
I was wondering if there is an efficient way to convert each element (each with different length) in my List to a column in a data frame to achieve my Desired_output?
I tried the following without success:
dplyr::bind_cols(List)
Note: This data is toy, a functional answer is appreciated.
List <- list(`1000`=letters[1:2], `2000`=letters[1:3], `3000`=letters[1:4])
Desired_output <-
data.frame(`1000`= c(letters[1:2],"",""),
`2000`= c(letters[1:3],""),
`3000`= letters[1:4])
You could try this.
library(purrr)
l <- list(`1000`=letters[1:2], `2000`=letters[1:3], `3000`=letters[1:4])
# get max lengths across all list items
max_len = max(lengths(l))
# using purrr::modify add as many empty characters needed until each
# item has the same number of rows as the item with the most
l = modify(l, function(f) {
f = c(f, rep("", max_len-length(f)))
})
as.data.frame(l)
X1000 X2000 X3000
1 a a a
2 b b b
3 c c
4 d

R Merge multiple vectors alternatively [duplicate]

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

How can I merge the different elements of the list? [duplicate]

This question already has answers here:
Paste multiple columns together
(11 answers)
Concatenate row-wise across specific columns of dataframe
(3 answers)
Closed 7 years ago.
I have a list/dataframe such as
a b c d e f g VALUE
1 0 1 0 0 0 1 934
what I wanted to do is to print,
1010001 without using for loop. so basically, take those integers as a string and merge them while printing?
I will define a function, which truncate the last value and paste all the other elements together. And then use "apply" on all the dataframe
cc <- data.frame(a=1,b=0,c=1,d=0,e=0,f=0,g=1,VALUE=934)
# This function contains the all the jobs you want to do for the row.
myfuns <- function(x, collapse=""){
x <- x[-length(x)] # truncate the last element
paste(x,collapse="") # paste all the integers together
}
# the second argument "MARGIN=1" means apply this function on the row
apply(cc,MARGIN=1,myfuns2) # output: "1010001"

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

how can i use a name (string) as variable? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
R: How to convert string to variable name?
If I do:
'a' = c(1:10)
a
[1] 1 2 3 4 5 6 7 8 9 10
here I assign a vector to a string (variable)
but i need to a do something like:
a = 'c10'
and then
a = c(1:10)
but the last a must to be c10
How can i do it?
Not sure what you're looking for but your first assignment doesn't need the c() and doesn't need quotes around the a.
a <- 1:10
if you want the last entry to be the string 'c10', you can get there a few different ways.
a <- c(1:9,'c10')
or
a <- 1:10
a[10] <- 'c10'
Or if Ben Bolker is on the right track:
a <- 'c10'
assign(a,1:10)

Resources