将行从一个df复制到另一个行中的每个行 [英] Copying row from one df into everyone row in another
本文介绍了将行从一个df复制到另一个行中的每个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数据框
一个df仅是包含列的一行:
One df being just one row containing columns:
Description | Location
Red | NH
第二个df包含多个包含列的行(假设是3行):
The second df containing multiple rows (lets say 3 rows) that contain columns:
Item_Num | Quantity | Rating |
01 | 2 | A |
02 | 5 | B |
03 | 4 | B+ |
我要创建它,以便第一个df中的第一行对
I want to make it so that the first row in the first df is repeating for every row in the second.
Description | Location | Item_Num | Quantity | Rating |
Red | NH | 01 | 2 | A |
Red | NH | 02 | 5 | B |
Red | NH | 03 | 4 | B+ |
我尝试玩循环,但遇到错误。加入他们并没有为我工作,也没有加入键
I tried playing around with loops but get an error. Joining them hasn't been working for me and there is no join key
推荐答案
一种选择是使用 tidyr
包中的> crossing 函数,该函数创建2个数据集的行的组合:
One option is to use crossing
function from tidyr
package, which creates combinations of rows of 2 datasets:
# example datasets
df1 = data.frame(Description = "Red",
Location = "NH")
df2 = data.frame(Item = 1:3,
Rating = c(20,25,30))
library(tidyr)
crossing(df1, df2)
# Description Location Item Rating
# 1 Red NH 1 20
# 2 Red NH 2 25
# 3 Red NH 3 30
这篇关于将行从一个df复制到另一个行中的每个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文