McMintz
McMintz

Reputation: 254

R loop and filter

So I don't how to go about asking this, I'm having a hard time googling as well so I figured asking with actual code would be better.

I'm new to R, I'm trying to teach myself and so far I'm picking up the basics, however I came across a situation now where I figured looping through would be a better option but I have no idea where to begin.

Right now I'm just making random csv files with all the states in them but multiple of the same entry just for fun.

I've learned with the library 'dplyr' I can filter by topic

hence:

temp <- filter(states, State="AL")

which I know assigns all rows that have State='AL' to temp

so instead of going state by state temp by temp, I'm trying to learn how I can develop a loop that can check the topic of the state and assign to temps dynamically so theoritically

temp1 <- filter(states, State="AL")
temp2 <- filter(states, State="CA")
....and so on

I'm just trying to figure out how I can loop through a files topic and assign it dynamically and then write each one dynamically to their own files.

write.csv(temp1, "AL.csv")

Even leading me to some where that can help me figure it out would be beneficial. I'm just stuck now and I know it's possible, or at least I think I do.

Upvotes: 2

Views: 6517

Answers (2)

Nick Kennedy
Nick Kennedy

Reputation: 12640

I would use base R’s split to split up your data and then purrr::iwalk to look through and write the files:

Sample data:

library(tidyverse)
states <- tibble(
  x = rnorm(1000),
  state = sample(state.abb, 1000, replace = TRUE)
)

Split and write:

split_states <- split(states, states$state)
iwalk(split_states, ~write_csv(.x, paste0(.y, ".csv"))

Note iwalk here is equivalent to:

for (state in names(split_states)) {
  write_csv(split_states[[state]], paste0(state, ".csv")
}

The upcoming 0.8.0 release of dplyr will have group_walk which should also work here, though I’ve not yet tried it.

Upvotes: 1

Dioney Blanco
Dioney Blanco

Reputation: 56

As far as I can see, you don't need to create every temp file, you just need to know which states you want to filter and then writing the csv file

To this purpose, now that you're familiar with dplyr you should install also readr which is part of the tidyverse package

To your question you must first have the unique states, first

# this will select the State column and get unique states
unique_states <- unique(states$State)

and then loop

# here i will get the i-th character value in unique states
# for example in the first iteration i = 'AL' if thats at the top of unique states
library(readr)
for( i in unique_states){

  write_csv(filter(states , State = i ), path = paste0(i, '.csv'))
}

Upvotes: 2

Related Questions