16
I have 2 data.frames, the first is a data.frame that contains stock data and a column with a unique identifier (column "ISIN"), as an example below:
Teste=data.frame(matrix(runif(20), nrow=5, ncol=4))
Teste$ISIN <- c("A1","A2","A3","A4","A5")
colnames(Teste) <- c("AVG_VOLUME","AVG_RETURN","VOL","PRICE","ISIN")
AVG_VOLUME AVG_RETURN VOL PRICE ISIN
Stock 1 0.7028197 0.09264265 0.002169411 100 A1
Stock 2 0.7154557 0.03314615 0.004839466 100 A2
Stock 3 0.4038030 0.04347487 0.003441471 100 A3
Stock 4 0.5392530 0.06414982 0.004482528 100 A4
Stock 5 0.8720084 0.09615865 0.008081017 100 A5
My second data.frame has an "ISIN" column but is not unique, as shown below :
Teste2 <- data.frame(matrix(runif(10), nrow=5, ncol=2))
Teste2$ISIN <- c("A1","A1","A3","A2","A2")
X1 X2 ISIN
1 0.0273074 0.6829592 A1
2 0.1928437 0.3768154 A1
3 0.9693224 0.3331828 A3
4 0.9434274 0.1549707 A2
5 0.6211476 0.3360101 A2
What I’d like to do is search the date.frame Test the value of the "AVG_VOLUME" column corresponding to the "ISIN" of the data.frame Teste2 and add this information as a new column in Teste2, resulting in :
X1 X2 ISIN AVG_VOLUME
1 0.0273074 0.6829592 A1 0.7028197
2 0.1928437 0.3768154 A1 0.7028197
3 0.9693224 0.3331828 A3 0.4038030
4 0.9434274 0.1549707 A2 0.7154557
5 0.6211476 0.3360101 A2 0.7154557
What is the best way to get this result, like a PROCV in excel ?
I like this pq already goes to the end of the new date.
– RiskTech
In the @carlosfigueira solution it is also possible to take to the end:
Teste3 <- merge(Teste2,Teste[,c("ISIN", "AVG_VOLUME")], by = "ISIN")
– Daniel Falbel