I have a function that looks like this
calc_df <- function(A_df, B_df){
C_df <- filter(A_df, Type == "Animal") %>%
left_join(B_df) %>%
as.numeric(C$Count)
Where I cannot get the last lime to work, the first 3 work properly, but I would like the last line to take the column "Count" from the new df calculated in the function and make it numeric. (Right now it is a character vector)
** I have to do this at the end of the function because before the filter command, the Count column contains letters and cannot be made as.numeric
Looks like you're using dplyr, and that you want to change or add a column. This is what the dplyr::mutate function does.
Replace
as.numeric(C$Count)
with
mutate(Count = as.numeric(Count))
to replace the old, non-numeric Count column with the coerced-to-numeric replacement.
As to why your code didn't work, there are a few problems:
dplyr is made for working with data frames, and the main dplyr functions (select, filter, mutate, summarize, group_by, *_join, ...) expect data frames as the first argument, and then return data frames. By piping the result of a left join into as.numeric, you are really calling as.numeric(unnamed_data_frame_from_your_join, C$Count), which clearly doesn't make much sense.
You are trying to reference a data frame called C inside a definition for a data frame called C_df, which I think you mean to be the same thing. There's two issues here: (1) the mismatch between the names C and C_df, and (2) you can't reference C_df inside it's own definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With