简体   繁体   中英

get a dataframe from a list of vectors and lists in R

I have a very difficult list, let's say that it has 3 elements.

The first one is a vector called "a", a<-c(1,2). The second one, called it "b" is another vector b<-c(3,4). Finally the third element is another list which size is 2 and in each element we have a data.frame like this one:

v1 v2 v3

5 5 5

5 5 5

5 5 5

I need to get this:

ab v1 v2 v3

1 3 5 5 5

1 3 5 5 5

1 3 5 5 5

2 4 5 5 5

2 4 5 5 5

2 4 5 5 5

Of course each element of the list has a bigger size, but it is works for two, it will do it for "n".

thanks!

We can use expand_grid

library(tidyr)
expand_grid(a, b)

-output

# A tibble: 6 x 4
#      a    v1    v2    v3
#  <dbl> <int> <int> <int>
#1     1     4     4     4
#2     1     4     4     4
#3     1     4     4     4
#4     2     4     4     4
#5     2     4     4     4
#6     2     4     4     4

data

a <- 1:2
b <- structure(list(v1 = c(4L, 4L, 4L), v2 = c(4L, 4L, 4L), v3 = c(4L, 
4L, 4L)), class = "data.frame", row.names = c(NA, -3L))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM