Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using as.numeric, with functions and pipes in R

Tags:

r

dplyr

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

like image 771
user4999605 Avatar asked Jul 14 '15 19:07

user4999605


1 Answers

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.

like image 100
Gregor Thomas Avatar answered Sep 22 '22 20:09

Gregor Thomas