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

4

1 回答 1

0

像这样:

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

subset请注意,以编程方式使用可能存在风险:为什么 `[` 比 `subset` 更好?. 不过这种用法很好。

于 2012-11-05T02:54:25.140 回答