Code to create function keeps getting error

I am trying to create a function that cleans this table but I keep getting errors when I actually try to apply the function on a data frame. Either the error is caused by the "filter(table_id != "extra") tab because it keeps saying that table_id could not be found OR the code stops at the next line of code with "filter(-notes)" saying that there is an invalid argument to unary operator. I absolutely cannot figure out what I'm doing wrong or why it sometimes works but then the code is stopped by the other error. Please help! I am so confused. This is my code:

clean <- function(dataframetable){
filter(table_id != "extra") %>% #removing "extra" row
filter(-notes) %>% #removing "notes" column
drop_na() %>% #removing all NAs
filter(height_in_inches > 0 & length > 0 & weight_in_pounds > 0) %>% #removing entries with negatives
mutate(height_in_inches = as.numeric(height_in_inches), #converting variables to numeric
length = as.numeric(length),
weight_in_pounds = as.numeric(weight_in_pounds)) %>%
mutate(new_var = weight_in_pounds * height_in_inches * length) #creating new variable

Any help is very much appreciated!

You need to pass dataframetable into filter.

1 Like

How would I do that?

dataframetable %>% filter(table_id != "extra") %>% # etc.
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.