Closed btskinner closed 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
I couldn't figure it out either and have done what KVA did. I couldn't find an answer even with the "hint."
@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):
|
operator when using list.files()
list.files()
separately, but combine the two output vectors into one, and then just loop through that listfiles_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.
Or alternately, check out the first line in issue #23.
Closing (see the example code in your repo), but reopen if you have other questions.
I've had a few questions about the pattern to use with
list.files()
.The
pattern
argument inlist.files()
takes a single string:pattern = "<string>"
. This means that neithernor
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.