I have the following code:
mdf[,c(4)]<- mdf$`CLAVE EMISORA` %in% BMV[[1]]$`CLAVE EMISORA`
mdf[,c(5)]<- mdf$`CLAVE EMISORA` %in% BMV[[2]]$`CLAVE EMISORA`
...............................................................
mdf[,c(13)]<- mdf$`CLAVE EMISORA` %in% BMV[[10]]$`CLAVE EMISORA`
It basically does a logical test of whether the content of each row of a column is contained within the column of a data frame inside list. The result is a logical test along a series of columns telling me where its contained, for that I have assigned columns 4-13 as a dichotomous variable of the presence or absence of each row in each list. But now I want to reduce the code and instead of mapping the results to different columns I want to use a single column with levels (1-10) where each level will be equal to the i list. I've tried with a nested for loop but it doesn't do the job.
for (i in mdf$`CLAVE EMISORA`){
for (x in 1:10)
if (mdf$`CLAVE EMISORA` %in% BMV[[x]]$`CLAVE EMISORA`)
mdf$`SECTOR` <- x
}
Example database:
`CLAVE EMISORA` `RAZON SOCIAL` SECTOR
<chr> <chr> <lgl>
1 AC ARCA CONTINENTAL, S.A.B. DE C.V. NA
2 ACCELSA ACCEL, S.A.B. DE C.V. NA
3 ACTINVR CORPORACION ACTINVER, S.A.B. DE C.V. NA
4 AEROMEX GRUPO AEROMÉXICO, S.A.B. DE C.V. NA
5 AGRIEXP AGRO INDUSTRIAL EXPORTADORA, S.A. DE C.V. NA
6 AGUA GRUPO ROTOPLAS, S.A.B. DE C.V. NA
7 AHMSA ALTOS HORNOS DE MEXICO, S.A. DE C.V. NA
8 ALEATIC ALEATICA, S.A.B. DE C.V. NA
9 ALFA ALFA, S.A.B. DE C.V. NA
10 ALPEK ALPEK, S.A.B. DE C.V. NA
Example list
CLAVE EMISORA RAZON SOCIAL
1 3391 TSURUHA HOLDINGS, INC.
2 4911 SHISEIDO COMPANY, LIMITED
3 ABEV AMBEV S.A.
4 AC ARCA CONTINENTAL, S.A.B. DE C.V.
5 ACBE AC BEBIDAS, S. DE R.L. DE C.V.
It seems like what you would want is to simply iterate over your 10 indices like this and perform your operation.
for (i in 1:10) {
mdf[,i + 3]<- mdf$`CLAVE EMISORA` %in% BMV[[i]]$`CLAVE EMISORA`
}
You could replace the i+3
with some programatic way of getting nicer column names if you wanted to.