简体   繁体   中英

R Statistical Function Failure with Data Frames

When using mean(), sd(), etc. functions with a dataframe, I'm getting an 'argument is not numeric or logical' error.

I created a simple frame from two vectors to test functionality (ie to use a stat function with a data frame).

str() gives the following:

'data.frame':   195 obs. of  2 variables:
 $ Births  : num  10.2 35.3 46 12.9 11 ...
 $ Internet: num  78.9 5.9 19.1 57.2 88 ...

Using the mean() function:

mean(frame2, na.rm=TRUE)

Gives:

Warning message: In mean.default(frame2, na.rm = TRUE) : argument is not numeric or logical: returning NA

I've seen previous advice to not use mean() with a data frame, which is fine, but not the point.

I'm going through the O'Reilly R Cookbook, and it claims you should be able to use mean() and sd() with a dataframe.

However, I can't make it work.

About your problem:

I dont have access to your book, or other learning resource but the best learning tool is R help. So to understand the type of arguments you can do ?mean and it says:

mean(x, trim = 0, na.rm = FALSE, ...)
Arguments

x   An R object. Currently there are methods for numeric/logical vectors and date, date-time and time interval objects. Complex vectors are allowed for trim = 0, only. 

So, as it explain it works the best for vectors , also based on this question , i think your book is a little old. Get your R version, and compare it with book.


It works well for me in this example:

dt<-data.frame(Births =sample(c(1:100),50),
           Internet =sample(c(1:100),50))

str(dt)
mean(dt$Births)

or even if i make the data as num still works:

dt<-data.frame(Births =as.numeric( sample(c(1:100),50)),
           Internet =as.numeric(sample(c(1:100),50)))

str(dt)
mean(dt$Births)

if you wish to pass your dataframe, and get general info in one go you can use summary function:

summary(iris)

Two options, first works if indeed all columns are numeric, 2nd just summarizes the numeric columns:

dt %>% dplyr::summarise_all(mean)
dt %>% dplyr::summarise_if(is.numeric, mean)


  Births Internet
1  47.86    47.52

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM