0

I have my code like this:

for (i in 1:b) {
carteraR[[i]]=subset(carteraR[[i]],RUN.FONDO=="8026" | RUN.FONDO=="8036" | RUN.FONDO=="8048" | RUN.FONDO=="8057" | RUN.FONDO=="8059" | RUN.FONDO=="8072" | RUN.FONDO=="8094" | 
RUN.FONDO=="8107" | RUN.FONDO=="8110" | RUN.FONDO=="8115" | RUN.FONDO=="8130" | RUN.FONDO=="8230" | RUN.FONDO=="8248" | RUN.FONDO=="8257" | RUN.FONDO=="8319")
}

Where b=length(carteraR), and class(carteraR[[i]])=data.frame. RUN.FONDO is one of the head of these data frames. This code is working fine but I want to save some lines.

What I want is something like:

for (i in 1:b) {
for (j in 1:length(A)){
carteraR[[i]]=subset(carteraR[[i]],RUN.FONDO==A[j])
}
}

And where A= "8026" "8036" "8048" "8057" ... "8319" ....... etc......

What should the code be like ?

Thx

Tomás Ayala
  • 107
  • 2
  • 15

1 Answers1

0

Like this:

carteraR <- lapply(carteraR, subset, RUN.FONDO %in% A)

Just be aware there can be risks with using subset in a programmatic way: Why is `[` better than `subset`?. This usage is fine though.

Community
  • 1
  • 1
flodel
  • 87,577
  • 21
  • 185
  • 223