简体   繁体   中英

R: 'Multiply' dataframe columns of strings

Not sure what this operation is called, and Google did not help.

Say I have two simple dataframes like this:

df1 <- data.frame(factor1 = c("a", "b", "c"))
df2 <- data.frame(factor2 = c("x", "y", "z"))


> df1
  factor1
1       a
2       b
3       c
> df2
  factor2
1       x
2       y
3       z

How can I get a dataframe formatted like this:

  factor1 factor2
1       a       x
2       a       y
3       a       z
4       b       x
5       b       y
6       b       z
7       c       x
8       c       y
9       c       z

I would think that this kind of operation might involve multiplying the dataframes, but this does not work:

> df1 * df2
  factor1
1      NA
2      NA
3      NA
Warning message:
In Ops.factor(left, right) : ‘*’ not meaningful for factors

It's a cartesian product of the two data frames, when there's no common names, you can use merge :

merge(df1, df2)   

#  factor1 factor2
#1       a       x
#2       b       x
#3       c       x
#4       a       y
#5       b       y
#6       c       y
#7       a       z
#8       b       z
#9       c       z

Or more explicitly:

merge(df1, df2, by=c())

According to ?merge , when there are no columns to join by, it returns a cartesian product of the two data frames:

If by or both by.x and by.y are of length 0 (a length zero vector or NULL), the result, r, is the Cartesian product of x and y, ie, dim(r) = c(nrow(x)*nrow(y), ncol(x) + ncol(y)).

Here is another option with expand.grid

Map(expand.grid, factor1 = df1, factor2 = df2)$factor
#    factor1 factor2
#1       a       x
#2       b       x
#3       c       x
#4       a       y
#5       b       y
#6       c       y
#7       a       z
#8       b       z
#9       c       z

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