Error when trying to calculate column average - R

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Error when trying to calculate column average - R



I have a dataframe so when I try to calculate the mean of column A I just write


mean(df$A)



and it works fine.



But when I try to calculate mean of only part of the data frame I get an error saying it isn't a number or logical value


df$A %>% filter(A=="some value") %>% mean(df$A)



The type of A is double. I also tried to convert it to numeric using


df$A <- as.numeric(as.character(df$A))



but it didn't work.





df$A by doing this we are converting dataframe column into a vector, and filter has no idea what "A" means. Try: names(mtcars[, "mpg", drop = FALSE]) vs names(mtcars$mpg)
– zx8754
Aug 8 at 7:49


df$A


filter


names(mtcars[, "mpg", drop = FALSE])


names(mtcars$mpg)





Ohh.. Thats the answer I needed!! Thanks.. So everytime I convert a df column into a vector, it changes its type ? but if I use names(mtcars[1]) it knows its mpg...
– Yarden Gur
Aug 8 at 8:14





Right, now compare mtcars[1] vs mtcars[[1]]. It is about how you subset, dataframe is just a list that can hold other types with the same length.
– zx8754
Aug 8 at 8:16



mtcars[1]


mtcars[[1]]





are the cars names like index values?? I can see choosing mtcars[[1]] keeps it numeric though..
– Yarden Gur
Aug 8 at 8:20





You could start with the comprehensive introduction to R and build on it. cran.r-project.org/doc/manuals/R-intro.pdf
– Roman Luštrik
Aug 8 at 9:10




2 Answers
2



Try df %>% filter(A==some value) %>% summarise(mean(A)).


df %>% filter(A==some value) %>% summarise(mean(A))



Note that the mean will be some value because of the filter.
Also, mean() works fine with objects of class double


some value


filter


mean()


double





Thanks!! it works.. it is really strange.. I don't understand why filtering by some criterion suddenly makes it non-numerical??
– Yarden Gur
Aug 8 at 7:44






Normally it shouldn't. Here is an example with the iris dataset: iris %>% filter(Sepal.Length == 4.8) %>% summarise(mean(Sepal.Length))
– ANG
Aug 8 at 7:50


iris


iris %>% filter(Sepal.Length == 4.8) %>% summarise(mean(Sepal.Length))





or df %>% filter(A==some value) %>% pull(A) %>% mean()
– Axeman
Aug 8 at 8:39


df %>% filter(A==some value) %>% pull(A) %>% mean()



Best would be to provide an example of your column A.



However, by just looking to your question the problem is in your magrittr-dplyr syntax.



base syntax:


mean(df$A[df$A == 'some value'])



dplyr with pipes:


df %>% filter(A==2) %>% summarise(., average = mean(A))



Careful with syntax and pipes, more info here.





That's actually much easier than using pipes!! Thanks! :)
– Yarden Gur
Aug 8 at 8:16






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS