Reputation: 323
I have a list of date frames:
x <- data.frame("SN" = 1:25,"Age" = 1:25,"Gender" = rep(c("Male","Female"),25))
y <- data.frame("SN" = 1:25,"Age" = 1:25,"Occupation" = rep(c("Barber","Doctor"),25))
z <- data.frame("SN" = 1:25,"Age" = 1:25)
list <- c(x,y,z)
I am attempting to bind the data frames together into one data frame, unlisted. I want to pull only specific columns to make the end data frame however.
So for instance, I want x,y, and z combined into one data frame with only SN and Age in the resulting data frame.
Is there a simple way of doing this?
Upvotes: 1
Views: 2452
Reputation: 16
I approached it the same way as igorkf but set my syntax slightly differently:
xyz <- x %>% select(SN, age) %>%
bind_rows(y %>% select(SN, age) %>%
bind_rows(z %>% select(SN, age))
Upvotes: 0
Reputation: 64
Too simple? Are Age and SB always in the 1 and 2 spot?
list <- as.data.frame(c(x[1:2],y[1:2],z[1:2]))
Upvotes: 1
Reputation: 181
Is this simple enough?
xyz <- bind_rows(select(x, SN, Age), select(y, SN, Age), select(z, SN, Age))
Upvotes: 3
Reputation: 20463
An approach with purrr
:
library(purrr)
lst %>% map_dfr(`[`, c("SN", "Age"))
Which says, map the extract [
function over the items "SN" and "Age" from each list, then bind together all those elements into a data.frame df
-- and hey, while you're binding them together, please bind the rows so dfr
.
Upvotes: 4
Reputation: 26343
Given
lst <- c(x,y,z)
it turns out that we can do
out <- unstack(stack(lst))
head(out)
# SN Age
#1 1 1
#2 2 2
#3 3 3
#4 4 4
#5 5 5
#6 6 6
Upvotes: 2
Reputation: 697
Here's a data.table
solution that will work with any data.table
that has any number of columns. It lines up the columns based on their names, and fills any blanks with NA
where needed. You will can always use that same approach for any number of data.tables.
library(data.table)
library(magrittr)
x <- data.frame("SN" = 1:25,"Age" = 1:25,"Gender" = rep(c("Male","Female"),25))
y <- data.frame("SN" = 1:25,"Age" = 1:25,"Occupation" = rep(c("Barber","Doctor"),25))
z <- data.frame("SN" = 1:25,"Age" = 1:25)
listy <- list(x,y,z)
a <- rbindlist(listy,
use.names = TRUE,
fill = TRUE) %>%
.[, .(SN, Age)]
Created on 2019-02-27 by the reprex package (v0.2.1)
Upvotes: 2
Reputation: 501
In case you want a general solution that will also work when you have more than three elements in your list of dataframes:
library(dplyr)
x <- data.frame("SN" = 1:25,"Age" = 1:25,"Gender" = rep(c("Male","Female"),25))
y <- data.frame("SN" = 1:25,"Age" = 1:25,"Occupation" = rep(c("Barber","Doctor"),25))
z <- data.frame("SN" = 1:25,"Age" = 1:25)
lst <- list(x,y,z)
df <- do.call(rbind, lapply(lst, select, SN, Age))
Upvotes: 4
Reputation: 3565
I don't got your question, but this helps?
bind_rows( (x %>% select(SN, Age)), (y %>% select(SN, Age)), z)
SN Age
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
11 11 11
12 12 12
13 13 13
14 14 14
15 15 15
16 16 16
17 17 17
18 18 18
19 19 19
20 20 20
21 21 21
22 22 22
23 23 23
24 24 24
25 25 25
26 1 1
27 2 2
28 3 3
29 4 4
30 5 5
31 6 6
32 7 7
33 8 8
34 9 9
35 10 10
36 11 11
37 12 12
38 13 13
39 14 14
40 15 15
41 16 16
42 17 17
43 18 18
44 19 19
45 20 20
46 21 21
47 22 22
48 23 23
49 24 24
50 25 25
51 1 1
52 2 2
53 3 3
54 4 4
55 5 5
56 6 6
57 7 7
58 8 8
59 9 9
60 10 10
61 11 11
62 12 12
63 13 13
64 14 14
65 15 15
66 16 16
67 17 17
68 18 18
69 19 19
70 20 20
71 21 21
72 22 22
73 23 23
74 24 24
75 25 25
76 1 1
77 2 2
78 3 3
79 4 4
80 5 5
81 6 6
82 7 7
83 8 8
84 9 9
85 10 10
86 11 11
87 12 12
88 13 13
89 14 14
90 15 15
91 16 16
92 17 17
93 18 18
94 19 19
95 20 20
96 21 21
97 22 22
98 23 23
99 24 24
100 25 25
101 1 1
102 2 2
103 3 3
104 4 4
105 5 5
106 6 6
107 7 7
108 8 8
109 9 9
110 10 10
111 11 11
112 12 12
113 13 13
114 14 14
115 15 15
116 16 16
117 17 17
118 18 18
119 19 19
120 20 20
121 21 21
122 22 22
123 23 23
124 24 24
125 25 25
Upvotes: 2