Make a subset with the same condition on different bases in R

Asked

Viewed 115 times

0

I’m willing to apply the same condition subset on different bases in the R. I thought the for was going to solve, but I did not succeed.

Follows programming used:

for(i in c("mar16", "jun16", "set16", "dez16", "mar17")){
 postos[i]<-subset(postos[i],postos[i]$SEGMENTO=="Bancos")
}

Can you use the for for this type of situation?

  • 1

    it would be interesting for you to provide a part of your database to reproduce your code.

  • It’s possible, but there are lower cost ways to do what you need. I believe your code has two errors, the first is to save on the same basis (the first postos[i]), the second is what is causing you more problems, does not have the [i] at last postos. Note that your possible result is a single-column dataframe.

1 answer

-1

You can use the functions lapply and split.

Ex: lapply(split(postos, postos$SEGMENTO), i).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.