edquant / edh7916

Course materials and website for EDH7916: Contemporary Research in Higher Education
https://edquant.github.io/edh7916/
3 stars 1 forks source link

Assignment 6: Q1.1 #22

Closed btskinner closed 4 years ago

btskinner commented 4 years ago

I've had a few questions about the pattern to use with list.files().

The pattern argument in list.files() takes a single string: pattern = "<string>". This means that neither

## won't work
files <- list.files(bys_dir, pattern = "bend_gate" | "niagara", full.names = TRUE)

nor

## won't work
files <- list.files(bys_dir, pattern = c("bend_gate" | "niagara"), full.names = TRUE)

will work. In each case, you have two strings going to the pattern argument, which can only take the single string.

If you want to use a vertical a pipe (|) as an OR statement, it needs to be inside the single string.

klvogelanderson commented 4 years ago

Hi Ben! Thank you so much for this hint, but I am completely stuck on question 1. Do you think we could please go over assignment 6 together in class on Tuesday? I ended up doing this for question 1, and this doesn't really answer your question:

files_bg <- list.files(bys_dir, pattern = "bend_gate", full.names = TRUE)
files_n <- list.files(bys_dir, pattern = "niagara", full.names = TRUE)            
files_bg
files_n

##For Bend Gate
df_bg_list <- list()
for (i in 1:length(files_bg)) {
    ## read in file (f) and store in list
    df_bg_list[[i]] <- read_csv(files_bg[i])    
}
df_bg <- df_bg_list %>% bind_rows()
df_bg

##For Niagara
df_n_list <- list()
for (i in 1:length(files_n)) {
    ## read in file (f) and store in list
    df_n_list[[i]] <- read_csv(files_n[i])    
}
df_n <- df_n_list %>% bind_rows()
df_n

##To combine into one table with both Bend Gate and Niagara
df_bg_n <- bind_rows(df_bg, df_n)
df_bg_n
LeeDelaino commented 4 years ago

I couldn't figure it out either and have done what KVA did. I couldn't find an answer even with the "hint."

btskinner commented 4 years ago

@klvogelanderson, I edited your comment to show the R code better.

It looks like your code will do the trick. There are two things you could do to reduce the number of lines of code (specifically the second loop):

  1. Use the | operator when using list.files()
  2. Continue to list.files() separately, but combine the two output vectors into one, and then just loop through that list
files_bg <- list.files(bys_dir, pattern = "bend_gate", full.names = TRUE)
files_n <- list.files(bys_dir, pattern = "niagara", full.names = TRUE)            

## concat
files <- c(files_bg, files_n)
files

### ... now just run your single loop / bind using the combined vector, files

To do version 1, have you looked at the {stringr} cheet sheet I linked in issue #6? Specifically, on the second page in the section called Alternates, it gives an example of how | should work.

btskinner commented 4 years ago

Or alternately, check out the first line in issue #23.

btskinner commented 4 years ago

Closing (see the example code in your repo), but reopen if you have other questions.