BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

Filling Unknown Values with a Placeholder-the for loop

https://app.dataquest.io/c/69/m/402/working-with-missing-data/10/filling-unknown-values-with-a-placeholder

The solution code and the use of mutate_at rather than mutate implies that c(v_col) and c(c_col) have length greater than 1 since mutate_at is used to edit multiple columns.

a) I do not see how this is possible since each iteration of the loop should only deal with one value of x at a time hence v_col should only have length 1.

b)I also dont understand why you cannot place the 2 mutate_at functions outside the for loop if c(v_col) edits all the relevant columns at once, how is each iteration of the for loop different for the muate_at functions what it does or does the loop just do the same thing five times. When I have tried this the database has no changes and I don’t understand why this happens.

I have attatched the code below that I am stuck on:

for (x in 1:5 ){
    v_col <- paste('vehicle', x,  sep = "_" )
    c_col <- paste('cause_vehicle', x,  sep = "_" )
    
    # create a logical vector for each column
    v_missing_logical  <-  is.na(mvc[v_col]) & !is.na(mvc[c_col])
    c_missing_logical  <-  !is.na(mvc[v_col]) & is.na(mvc[c_col])
    
    # replace the values matching the logical vector for each column
    mvc <- mvc %>%
        mutate_at(c(v_col), function(x) if_else(v_missing_logical,"Unspecified", v_col ))
    
    mvc <- mvc %>%
        mutate_at(c(c_col), function(x) if_else(c_missing_logical,"Unspecified", c_col ))
}