I would like to add new columns into another data frame at the end of a pipe with mutate using a condition. If the length of the variable is zero, then add a dash in the column, otherwise add the content. This is part of a loop where I bind the resulting data frames, hence all lists have only one item and the data frames here only have one row.
Is it possible with mutate to add a column into a data frame other than the one being used in the pipe?
I tried solving this using tips I found here: Combine mutate with conditional values
Example code:
x <- "bbb"
y <- ""
end <- data.frame(a_col="aaa")
end <- x %>%
mutate (end, x_col = case_when(length()==0 ~ '-',
length()!=0 ~ .))
end <- y %>%
mutate (end, y_col = case_when(length()==0 ~ '-',
length()!=0 ~ .))
With both, I get this: "Error in UseMethod("mutate_") : no applicable method for 'mutate_' applied to an object of class "character""
Expected result for the 'end' data frame:
a_col x_col y_col
1 aaa bbb -