I am fairly new to Rstudio and am trying to write a function where I check if the string in a row of cells from Book 1 match the string inside a row of cells from a different excel sheet, Book2 and if they do not delete the row from Book1. I think my issue is using != to check what is inside the string but I’m unsure where to go from here.
This is what I have so far
for (i in Book1[,1]
{ if i != Book2[,1] {
Book1[-c(i)] }}
I was expecting my output to remove the rows from Book1 that are not in Book2.
>Solution :
A lot to unpack here. First you haven’t used a function but a loop, which is fine just terminology. Second, in R bracket indexing is [row,column] so you are indexing an entire colum where you state you are looking for rows. I think you are looking to match the value in each cell which requires double indexing as I’ve shown below. Also you should include some data so that the folks on here can reproduce your actual problem. I have made up some to get started. Hopefully this helps.
book1 <- data.frame(x=round(rnorm(10,5,1)),
y=round(rnorm(10,10,2)))
book2 <- data.frame(x=round(rnorm(10,5,1)),
y=round(rnorm(10,10,2)))
book1
book2
for (i in 1:10) { # Note this is the number of rows...1 - 10
for (j in 1:2) {# Notre this is the number of columns...1 - 2
if(book2[i,j]!=book1[i,j]) { # if the value in book2 row i column j is not equal to the value in book1 row i column j
book1[i,j] <- NA # set book1 row i column j equal to NA
}
}
}
book1 # view the result