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 Six - binding rows issue #23

Closed jharsell closed 4 years ago

jharsell commented 4 years ago

Hi All,

Definitely losing the war with the assignment this week. I seem to get a bit of traction (as in no red) running the code below; however, I get the following error "Error: Argument 1 must have names". Can anyone pinpoint why I have that error or any additional errors in the code for all of question one below?

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

## check
files_BGN

## init list
df_BGN_list <- list(files_BGN)

## 2.
## use loop to read in files
for (i in 1:length(files_BGN)) {
    df_bgn_list[[i]] <- read_csv(files_BGN[i]) 
    mutate(df_BGN_list, relative_path =="../data/schools/by_test/bend_gate_1980.csv")
}

## bind our list to single data frame
df_BGN <- df_BGN_list %>%
    bind_rows()

Many thanks for any help or insight you can provide! Jaime

btskinner commented 4 years ago

This looks good and correctly uses the | operator.

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

## check
files_BGN

Moving to the next bit

## init list
df_BGN_list <- list(files_BGN)

you don't need to put files_BGN inside list(): you can just use list() with empty parentheses, which will make sure that R knows df_BGN_list is a list object with, for the moment, nothing in it.

Moving forward:

## 2.
## use loop to read in files
for (i in 1:length(files_BGN)) {
    df_bgn_list[[i]] <- read_csv(files_BGN[i]) 
    mutate(df_BGN_list, relative_path =="../data/schools/by_test/bend_gate_1980.csv")
}

First, your loop looks correctly set up as does your first bit of code inside the loop. My guess is that if you just deleted the second line inside the loop, your code runs. Yes?

The second line inside the loop,

mutate(df_BGN_list, relative_path =="../data/schools/by_test/bend_gate_1980.csv")

is where I see a couple of issues. First, remember that {dplyr} works with data frames (tibbles) and that df_BNG_list is a list. It contains data frames, but as a whole is a list. Instead of calling df_BGN_list, could you use a %>% to connect read_csv() and mutate()? That way, you can leave out the first .data argument inside mutate() and just add your new column.

Two last things.

  1. When adding a column with mutate(), remember that in general, = means assignment (like <-) whereas == is a test for equality.
  2. You have a hard coded path right now, "../data/schools/by_test/bend_gate_1980.csv", which will give the same answer each iteration of the loop. You can reuse code you already have inside your loop to give you the unique path to each file.
btskinner commented 4 years ago

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