简体   繁体   中英

R: How to create a data frame with one observation for each combination of factors

I hope all is well. I am writing with regards to a very specific question in R to which I so far was not able to find a solution online. If the question has already been addressed somewhere else, I am sorry for bothering you but would appreciate if you could provide me with the link.

I have 3 separate data sets:

The first one is a list of companies. The second one is a list of years. The third one is a list of countries.

My objective is now to merge these 3 data sets into a new data frame. The final data frame should create a data row for each combination of these 3 variables . This is the reason why I cannot use the merge() function. As a next step, I want to match data along this newly created data frame.

Thank you ever you much for your support - and again sorry if the question has already been addressed elsewhere!

Try merge :

A <- data.frame(Companies = LETTERS[1:3])
B <- data.frame(Years = 2000:2002)
C <- data.frame(Countries = c("GER", "UK", "US"))

X <- merge(merge(A, B), C)
X

   Companies Years Countries
1          A  2000       GER
2          B  2000       GER
3          C  2000       GER
4          A  2001       GER
5          B  2001       GER
6          C  2001       GER
7          A  2002       GER
8          B  2002       GER
9          C  2002       GER
10         A  2000        UK
...

If you have more than 3 variables/factors you could write your own merge function like this:

mergeN <- function(dfs = NULL) {
  if(is.null(dfs) | class(dfs) != "list") stop("'dfs' is not a list!")
  if(length(dfs) > 1) {
    dfs[[1]] <- merge(dfs[[1]], dfs[[2]]) 
    dfs[[2]] <- NULL
    Recall(dfs)
  } else {
    return(dfs[[1]]) 
  }
}

D <- data.frame(Products = letters[24:26])
E <- data.frame(Divisions = c(100,200,300))

mergeN(list(A, B, C, D, E))

This will give you a dataframe of all 3^5 = 243 combinations.

Update due to comments:

A <- data.frame(Companies = LETTERS[1:3])
B <- data.frame(Years = 2000:2002)
C <- data.frame(Countries = c("GER", "UK", "US"))

X <- merge(merge(A, B), C)

Y <- data.frame(Companies = LETTERS[1:3], Years = rep(2000,3), Countries = c("GER", "UK", "US"), Revenues = c(20433,23255,32164))

merge(X, Y, all=T)

     Companies Years Countries Revenues
1          A  2000       GER    20433
2          A  2000        UK       NA
3          A  2000        US       NA
4          A  2001       GER       NA
5          A  2001        UK       NA
6          A  2001        US       NA
7          A  2002       GER       NA
8          A  2002        UK       NA
9          A  2002        US       NA
10         B  2000       GER       NA
11         B  2000        UK    23255
12         B  2000        US       NA
13         B  2001       GER       NA
14         B  2001        UK       NA
15         B  2001        US       NA
16         B  2002       GER       NA
17         B  2002        UK       NA
18         B  2002        US       NA
19         C  2000       GER       NA
20         C  2000        UK       NA
21         C  2000        US    32164
22         C  2001       GER       NA
23         C  2001        UK       NA
24         C  2001        US       NA
25         C  2002       GER       NA
26         C  2002        UK       NA
27         C  2002        US       NA

(If you want NA's to be zero: Z[is.na(Z)] <- 0 )

Borrowing input data frames from @Martin, here's an approach that involves placing all your data frames in a list , and then using Reduce() :

d1 <- data.frame(Companies = LETTERS[1:3])
d2 <- data.frame(Years = 2000:2002)
d3 <- data.frame(Countries = c("GER", "UK", "US"))
d4 <- data.frame(Companies = LETTERS[1:3], Years = rep(2000,3), Countries = c("GER", "UK", "US"), Revenues = c(20433,23255,32164))

d <- list(d1, d2, d3, d4)
merged_dat <- Reduce(function(...) merge(..., all=T), d)
head(merged_dat)
#>   Companies Years Countries Revenues
#> 1         A  2000       GER    20433
#> 2         A  2000        UK       NA
#> 3         A  2000        US       NA
#> 4         A  2001       GER       NA
#> 5         A  2001        UK       NA
#> 6         A  2001        US       NA

I prefer this because it generalises to as many data frames as you might have.

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