2017-09-01 57 views
0

我试图初始化基于尺寸和维数由矢量索引所定义的数据帧: 例如 -从由矢量定义的尺寸的已知列表初始化数据帧

ind1 <- c('red', 'blue') 
ind2 <- c('circle', 'square', 'triangle') 

myFun(ind1,ind2) # this is the great function that yields your result:

red circle 
red square 
red triangle 
blue circle 
blue square 
blue triangle 
+0

完美,这个执行奇妙。如果您将此作为答案,我会将其标记为正确! – Sarobinson

+0

@Psidom让它成为一个答案伙计:) – Wen

回答

1

你需要的是expand.grid其中从输入向量的所有组合创建一个数据帧

expand.grid(ind1, ind2) 

# Var1  Var2 
#1 red circle 
#2 blue circle 
#3 red square 
#4 blue square 
#5 red triangle 
#6 blue triangle 

此外,如果你使用data.table你可以使用交叉联接CJ

data.table::CJ(ind1, ind2) 

#  V1  V2 
#1: blue circle 
#2: blue square 
#3: blue triangle 
#4: red circle 
#5: red square 
#6: red triangle