0

I've got a Data Frame (df) with 4 Columns and n rows

df <- structure(list(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 1L, 2L), y = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L), pch = c(9L, 10L, 11L, 7L, 12L, 9L, 
7L, 5L, 8L, 1L, 8L, 2L, 5L, 8L, 5L), col = c(7L, 8L, 3L, 3L, 
4L, 6L, 3L, 4L, 2L, 1L, 7L, 5L, 4L, 7L, 6L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15"))

    x y pch col
1   1 1   9   7
2   2 1  10   8
3   3 1  11   3
4   4 1   7   3
5   5 1  12   4
6   6 1   9   6
7   7 1   7   3
8   8 1   5   4
9   9 1   8   2
10 10 1   1   1
11 11 1   8   7
12 12 1   2   5
13 13 1   5   4
14  1 2   8   7
15  2 2   5   6

and I want to compare the X and Y with another Data Frame 1x1 (df2)

df2 <- structure(list(V1 = 7, V2 = 1), class = "data.frame", row.names = c(NA, 
-1L))

  V1 V2
1  7  1

and if it is the same I want to take the entry( in this case number 7) to draw it into my grid with the pch and col which are written in the first Data Frame.

My attempt was compare it with a if loop but I don't know how to get the right column from the first Data frame. In this case x = 7 y = 1 pch = 7 and col = 3

if(input$V1 == playfield$x && input$V2 == playfield$y) {

}

Appreciate every help or idea.

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Does this answer your question? [Compare if two dataframe objects in R are equal?](https://stackoverflow.com/questions/10592148/compare-if-two-dataframe-objects-in-r-are-equal) – Majid Hajibaba Dec 25 '21 at 12:04

2 Answers2

1

You can just do:

new_df <- playfield[playfield$x == input$V1 & playfield$y == input$V2,]
deschen
  • 10,012
  • 3
  • 27
  • 50
1

You could use right_join from dplyr

library(dplyr)

right_join(df, df2, by=c("x"="V1", "y"="V2"))

output:

  x y pch col
1 7 1   7   3

data:

df <- structure(list(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 1L, 2L), y = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L), pch = c(9L, 10L, 11L, 7L, 12L, 9L, 
7L, 5L, 8L, 1L, 8L, 2L, 5L, 8L, 5L), col = c(7L, 8L, 3L, 3L, 
4L, 6L, 3L, 4L, 2L, 1L, 7L, 5L, 4L, 7L, 6L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15"))

df2 <- structure(list(V1 = 7, V2 = 1), class = "data.frame", row.names = c(NA, 
-1L))
TarJae
  • 72,363
  • 6
  • 19
  • 66