I would like to check if the (ExDate-n) in the Dividend Table exist in the Date Column in the StockPrice Table where n is an incremental variable. If the (ExDate-n) exists in the StockPrice Table, I will assign Exminus1 as ExDate-n It will first start with n = 1 (i.e. yesterday) and finish once the (ExDate-n) exists in the StockPrice Table. I therefore created a for loop but it didn't work out.
I tried the for loop with if statement and %in% operator but it didn't work.
This is the original tables.
Stock table:

Dividend table:

Final output:

#Install necessary packages
install.packages('quantmod')
install.packages('plyr')
install.packages('dplyr')
library('quantmod')
library('plyr')
library('dplyr')
####################################   Input No.    ########################################
StockNo <- "6823.hk"
startDate <- "2001-01-01"
####################################   Input No.    ########################################
# Stock price Data
stockData <- new.env() #Make a new environment for quantmod to store data in
startDate = as.Date(StartDate) #Specify period of time we are interested in
endDate = Sys.Date()
tickers <- c(StockNo) #Define the tickers we are interested in
StockPrice <- getSymbols(tickers, env = stockData, src = "yahoo", from = startDate, to = endDate, auto.assign = FALSE)
StockPrice <- as.data.frame(StockPrice)
StockPrice <- cbind(Date=as.Date(rownames(StockPrice)),StockPrice)
#Dividend data
Dividend <- getDividends(tickers, env = stockData, src = "yahoo", from = startDate, to = endDate, auto.assign = FALSE)
Dividend <- as.data.frame(Dividend)
Dividend <- cbind(ExDate=as.Date(rownames(Dividend)),Dividend)
# Create Ex Date - 1
#!!!!!!!!!!!!!!!!!!!   Here is where I had issue with 
for (previous in (1:6))
{ 
  if ( (Dividend$ExDate - previous ) %in% StockPrice$Date) 
  { 
    Dividend$Exminus1 <- Dividend$ExDate-previous 
break;
    }
  previous <- previous+1
}
#New <- Dividend[,c(3,1,2)]
#Dividend
#Combine Stock Price & Dividend
CombineTable <- left_join (StockPrice, Dividend , by = c("Date"="Exminus1"))
#Calculate Yield
CombineTable$Yield <- CombineTable[,9]/CombineTable[,5]
#Preapre Month / Year for more segments
CombineTable$Month <- format(as.Date(CombineTable$ExDate, format="%d/%m/%Y"),"%m")
CombineTable$Year <- format(as.Date(CombineTable$ExDate, format="%d/%m/%Y"),"%Y")
tail(CombineTable)
## Get Data with Dividend Only
DivOnlyData <- CombineTable [ !is.na(CombineTable$ExDate) ,]
DivOnlyData
I tried the ifelse method but still no luck. The new column Dividend$Exminus1 always returns Dividend$ExDate - 5 even though the nested ifelse statement is correct when I only ran it.
ifelse ( (Dividend$ExDate - 1) %in% StockPrice$Date ,Dividend$Exminus1 <- Dividend$ExDate-1 , 
         ifelse ( (Dividend$ExDate - 2) %in% StockPrice$Date , Dividend$Exminus1 <- Dividend$ExDate-2, 
                  ifelse ( (Dividend$ExDate - 3) %in% StockPrice$Date , Dividend$Exminus1 <- Dividend$ExDate-3  ,
                           ifelse ( (Dividend$ExDate - 4) %in% StockPrice$Date , Dividend$Exminus1 <- Dividend$ExDate-4  ,
                                  ifelse ( (Dividend$ExDate - 5) %in% StockPrice$Date , Dividend$Exminus1 <- Dividend$ExDate-5 ,NA)))))  
