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
return(dataframetable)
}
Any help is very much appreciated!