Home > Software design >  Creating a data frame with every combination of two vectors
Creating a data frame with every combination of two vectors

Time:11-14

I'm trying to create a data frame from other data, my current data includes 20 countries, 10 years from 2020-2030. I wish to now create a new df containing each country and each year separately which would look something like this;

desired df:

Albania 2021
Albania 2022
...... continued to 2030, then
Algeria 2021
Algeria 2022 and so on

data needed for this:

CountryNames
"Albania", "Algeria", "Bosnia and Herzegovina" "Croatia", "Cyprus", "Egypt, Arab Rep.", "France", "Greece", "Israel",  "Italy", "Lebanon", "Libya", "Malta", "Montenegro", "Morocco", "Slovenia", "Spain", "Syrian Arab. Rep", "Tunisia", "Turkey"
future_years
2021, 2022, 2023, 2024, 2025, 2025, 2026, 2027, 2028, 2029, 2030

CodePudding user response:

I think you just want expand.grid:

all_df <- expand.grid(future_years = c(2021, 2022, 2023, 2024, 2025, 2025, 2026, 2027, 2028, 2029, 2030),
            CountryNames = c("Albania", "Algeria", "Bosnia and Herzegovina", 
                             "Croatia", "Cyprus", "Egypt, Arab Rep.", "France", "Greece",
                             "Israel",  "Italy", "Lebanon", "Libya", "Malta", "Montenegro",
                             "Morocco", "Slovenia", "Spain", "Syrian Arab. Rep", "Tunisia",
                             "Turkey"))[,c("CountryNames", "future_years")]

head(all_df, 10)
#>    CountryNames future_years
#> 1       Albania         2021
#> 2       Albania         2022
#> 3       Albania         2023
#> 4       Albania         2024
#> 5       Albania         2025
#> 6       Albania         2025
#> 7       Albania         2026
#> 8       Albania         2027
#> 9       Albania         2028
#> 10      Albania         2029

Created on 2022-11-13 by the reprex package (v2.0.1)

CodePudding user response:

Use complete from tidyr:

With your given 2 vectors:

CountryNames <- c("Albania", "Algeria", "Bosnia and Herzegovina","Croatia", "Cyprus", "Egypt, Arab Rep.", "France", "Greece", "Israel",  "Italy", "Lebanon", "Libya", "Malta", "Montenegro", "Morocco", "Slovenia", "Spain", "Syrian Arab. Rep", "Tunisia", "Turkey")
future_years <- c(2021, 2022, 2023, 2024, 2025, 2025, 2026, 2027, 2028, 2029, 2030)

We could use complete:

library(tidyr)

df %>%
  complete(CountryNames, future_years = 2021:2030) %>%
  as.data.frame()
       CountryNames future_years
1                  Albania         2021
2                  Albania         2022
3                  Albania         2023
4                  Albania         2024
5                  Albania         2025
6                  Albania         2026
7                  Albania         2027
8                  Albania         2028
9                  Albania         2029
10                 Albania         2030
11                 Algeria         2021
12                 Algeria         2022
13                 Algeria         2023
14                 Algeria         2024
15                 Algeria         2025
16                 Algeria         2026
17                 Algeria         2027
18                 Algeria         2028
19                 Algeria         2029
20                 Algeria         2030
21  Bosnia and Herzegovina         2021
22  Bosnia and Herzegovina         2022
23  Bosnia and Herzegovina         2023
24  Bosnia and Herzegovina         2024
25  Bosnia and Herzegovina         2025
26  Bosnia and Herzegovina         2026
27  Bosnia and Herzegovina         2027
28  Bosnia and Herzegovina         2028
29  Bosnia and Herzegovina         2029
30  Bosnia and Herzegovina         2030
31                 Croatia         2021
32                 Croatia         2022
33                 Croatia         2023
34                 Croatia         2024
35                 Croatia         2025
36                 Croatia         2026
37                 Croatia         2027
38                 Croatia         2028
39                 Croatia         2029
40                 Croatia         2030
41                  Cyprus         2021
42                  Cyprus         2022
43                  Cyprus         2023
44                  Cyprus         2024
45                  Cyprus         2025
46                  Cyprus         2026
47                  Cyprus         2027
48                  Cyprus         2028
49                  Cyprus         2029
50                  Cyprus         2030
51        Egypt, Arab Rep.         2021
52        Egypt, Arab Rep.         2022
53        Egypt, Arab Rep.         2023
54        Egypt, Arab Rep.         2024
55        Egypt, Arab Rep.         2025
56        Egypt, Arab Rep.         2026
57        Egypt, Arab Rep.         2027
58        Egypt, Arab Rep.         2028
59        Egypt, Arab Rep.         2029
60        Egypt, Arab Rep.         2030
61                  France         2021
62                  France         2022
63                  France         2023
64                  France         2024
65                  France         2025
66                  France         2026
67                  France         2027
68                  France         2028
69                  France         2029
70                  France         2030
71                  Greece         2021
72                  Greece         2022
73                  Greece         2023
74                  Greece         2024
75                  Greece         2025
76                  Greece         2026
77                  Greece         2027
78                  Greece         2028
79                  Greece         2029
80                  Greece         2030
81                  Israel         2021
82                  Israel         2022
83                  Israel         2023
84                  Israel         2024
85                  Israel         2025
86                  Israel         2026
87                  Israel         2027
88                  Israel         2028
89                  Israel         2029
90                  Israel         2030
91                   Italy         2021
92                   Italy         2022
93                   Italy         2023
94                   Italy         2024
95                   Italy         2025
96                   Italy         2026
97                   Italy         2027
98                   Italy         2028
99                   Italy         2029
100                  Italy         2030
101                Lebanon         2021
102                Lebanon         2022
103                Lebanon         2023
104                Lebanon         2024
105                Lebanon         2025
106                Lebanon         2026
107                Lebanon         2027
108                Lebanon         2028
109                Lebanon         2029
110                Lebanon         2030
111                  Libya         2021
112                  Libya         2022
113                  Libya         2023
114                  Libya         2024
115                  Libya         2025
116                  Libya         2026
117                  Libya         2027
118                  Libya         2028
119                  Libya         2029
120                  Libya         2030
121                  Malta         2021
122                  Malta         2022
123                  Malta         2023
124                  Malta         2024
125                  Malta         2025
126                  Malta         2026
127                  Malta         2027
128                  Malta         2028
129                  Malta         2029
130                  Malta         2030
131             Montenegro         2021
132             Montenegro         2022
133             Montenegro         2023
134             Montenegro         2024
135             Montenegro         2025
136             Montenegro         2026
137             Montenegro         2027
138             Montenegro         2028
139             Montenegro         2029
140             Montenegro         2030
141                Morocco         2021
142                Morocco         2022
143                Morocco         2023
144                Morocco         2024
145                Morocco         2025
146                Morocco         2026
147                Morocco         2027
148                Morocco         2028
149                Morocco         2029
150                Morocco         2030
151               Slovenia         2021
152               Slovenia         2022
153               Slovenia         2023
154               Slovenia         2024
155               Slovenia         2025
156               Slovenia         2026
157               Slovenia         2027
158               Slovenia         2028
159               Slovenia         2029
160               Slovenia         2030
161                  Spain         2021
162                  Spain         2022
163                  Spain         2023
164                  Spain         2024
165                  Spain         2025
166                  Spain         2026
167                  Spain         2027
168                  Spain         2028
169                  Spain         2029
170                  Spain         2030
171       Syrian Arab. Rep         2021
172       Syrian Arab. Rep         2022
173       Syrian Arab. Rep         2023
174       Syrian Arab. Rep         2024
175       Syrian Arab. Rep         2025
176       Syrian Arab. Rep         2026
177       Syrian Arab. Rep         2027
178       Syrian Arab. Rep         2028
179       Syrian Arab. Rep         2029
180       Syrian Arab. Rep         2030
181                Tunisia         2021
182                Tunisia         2022
183                Tunisia         2023
184                Tunisia         2024
185                Tunisia         2025
186                Tunisia         2026
187                Tunisia         2027
188                Tunisia         2028
189                Tunisia         2029
190                Tunisia         2030
191                 Turkey         2021
192                 Turkey         2022
193                 Turkey         2023
194                 Turkey         2024
195                 Turkey         2025
196                 Turkey         2026
197                 Turkey         2027
198                 Turkey         2028
199                 Turkey         2029
200                 Turkey         2030
  •  Tags:  
  • r
  • Related