Chaining - R

Asked

Viewed 97 times

0

I have two dates frames, one containing product data and the other containing store data, however I do not have reference variables between them (which would not allow me an Inner Join), my need is to present for each of these products in question the code of each of the stores, as in the case below:

LOJA Produto

1    camera 

1    Smartphone

1    TV

2    camera

2    Smartphone

2    TV

.    .

.    .

. .

.

2 answers

3

With Rafael’s data, it will be simpler to use the function expand.grid.

LOJA = 1:3
Produtos = c("camera", "Smartphone", "TV")

res <- expand.grid(LOJA = LOJA, Produtos = Produtos)[, 2:1]
res

2


If your information is similar to what is in the expected result,

LOJA = 1:3
Produtos = c("camera", "Smartphone", "TV")

one merge resolves.

dados <- merge(LOJA, Produtos)
library(dplyr)
dados <- arrange(dados, x)
names(dados) <- c("LOJA", "Produtos")

Here the result of the above code

  LOJA   Produtos
     1     camera
     1 Smartphone
     1         TV
     2     camera
     2 Smartphone
     2         TV
     3     camera
     3 Smartphone
     3         TV

Browser other questions tagged

You are not signed in. Login or sign up in order to post.