Home > database >  Error message - why does it say "object not found"?
Error message - why does it say "object not found"?

Time:03-04

I'm very new to R and stackoverflow, so I apologize ahead of time if I'm breaking any etiquette somehow.

I have only typed a few lines of code so far, following a book tutorial. The book is Statistical Modeling: A Fresh Approach, printed in 2012, so I'm not sure what version of R it has. I am using R 4.0.0.

All I have written so far is:

cherryBlossom2008 <- read.csv("Cherry-Blossom-2008.csv")
names(cherryBlossom2008)

this produces:

[1] "position" "division" "total" "name" "age" "place" "net" "gun" "sex"

Next I typed:

mean(age, data=cherryBlossom2008)

I immediately get an error message that says:

Error in mean(age, data = cherryBlossom2008) : object 'age' not found

I'm not sure how this is possible. 'age' is in cherryBlossom2008. My book says that I would get that error message if I failed to define "data" when using the "mean" command, but as you can see I did define "data", so I don't understand how else I would define 'age'.

CodePudding user response:

I haven't referred the book so I may be loosing some context here but based on your explanation and description I think there are few possibilities here.

  1. Book is wrong and this is some kind of mistake.

mean is an internal command. You can look at the documentation (?mean) and notice there is no data argument defined in mean.

To get mean you can use -

mean(cherryBlossom2008$age, na.rm = TRUE)
  1. You are supposed to use mean command present in some other library and not base.

  2. You have to define your own mean function and not use the internal one.

mean <- function(col, data) {
  base::mean(data[[deparse(substitute(col))]], na.rm = TRUE)
}

In which case - mean(age, data=cherryBlossom2008) will work.

For eg - with mtcars dataset -

mean(mpg, data = mtcars)
#[1] 20.09062

However, this option is very unlikely.

  • Related