简体   繁体   English

R:“乘以”字符串的数据框列

[英]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

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: 根据?merge ,当没有要连接的列时,它将返回两个数据帧的笛卡尔积

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)). 如果by.x和by.y或长度均为0(长度为零的向量或NULL),则结果r为x和y的笛卡尔积,即dim(r)= c(nrow(x )* nrow(y),ncol(x)+ ncol(y))。

Here is another option with expand.grid 这是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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