gpt4 book ai didi

R:两个数据帧的笛卡尔积的任何函数?

转载 作者:行者123 更新时间:2023-12-04 10:12:56 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to do cross join in R?

(9 个回答)


5年前关闭。




我需要做两个数据框的笛卡尔积。例如,

 A = id weight type
10 20 a
10 30 b
25 10 c
B = date report
2007 y
2008 n

那么 C 在做 A 和 B 的笛卡尔积之后会像
 C =  id weight type  date  report
10 20 a 2007 y
10 20 a 2008 n
10 30 b 2007 y
10 30 b 2008 n
25 10 c 2007 y
25 10 c 2008 n

因为有些 id 在 A 中是相同的,所以我不能使用像
C <- merge(A$id,B$date)
C <- merge(C,A,by="id")
C <- merge(C,B,by="date")

这种方式会产生更多的行。有人能帮我离开这里吗?谢谢

最佳答案

merge(A, B) ,如果没有列连接两者,默认情况下应该这样做,不是吗?

来自 ?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, i.e., dim(r) = c(nrow(x)*nrow(y), ncol(x) + ncol(y)).



诚然,这确实需要一个人知道查看 ?merge . R 中严重缺乏基于上下文的搜索;偶 rseek不会立即提供此信息。

关于R:两个数据帧的笛卡尔积的任何函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39198078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com