0

This is the first dataframe:

  df1 <- data.frame(pid = c(12,13,14,15,16,17,32,44,3,4,59,2,91), name = c(product1,product2,product3,product,product4,product5,product6,product7,product8,product9,product10,product11,product12,product43))

I would like to extract a new subset of df1 using as index values the pid of df2:

df2 <- data.frame(pid = c(12,13,14,4,2))

Example of subset:

  df1 <- data.frame(pid = c(12,13,14,15,16,17,32,44,2,4,59,2,91), name = c(product1,product2,product3,product10,product12))

how could I make it?

Elfan
  • 25
  • 7

1 Answers1

1

Not sure of what you need because your example of subset dataframe is not valid. However, this might be a solution for what you're looking for:

df1 <- data.frame(pid = c(12,13,14,15,16,17,32,44,3,4,59,2,91), 
                 name = c("product1","product2","product3","product4",
                 "product5","product6","product7","product8","product9",
                 "product10","product11","product12","product43"))
df2 <- data.frame(pid = c(12,13,14,4,2))

require(dplyr)
right_join(df1,df2)

Result:

  >right_join(df1,df2)
  Joining, by = "pid"

  pid      name
1  12  product1
2  13  product2
3  14  product3
4   4 product10
5   2 product12
La Cordillera
  • 410
  • 5
  • 17