Dear I use *flights* database library(nycflights13) The following code is working as colMeans(flights[2]) * 6.54851* but other code is not working as colMeans(flights$month) *Error in colMeans(flights$month) : * * 'x' must be an array of at least two dimensions* *flights[2]* is equal to the *month *column in database *Sincerely* Engin YILMAZ [[alternative HTML version deleted]]
On 04/11/2020 8:26 a.m., Engin Y?lmaz wrote:> Dear > I use *flights* database library(nycflights13) > > The following code is working as > > colMeans(flights[2]) > > * 6.54851* > > but other code is not working as > > colMeans(flights$month) > > *Error in colMeans(flights$month) : * > * 'x' must be an array of at least two dimensions* > > *flights[2]* is equal to the *month *column in databaseNo, flights[2] is a dataframe with one column. flights[[2]] would be the month column. Duncan Murdoch
Hi, You might want to become familiar with the ?str and ?dim functions, which can help you identify the structure of the objects that you are passing to colMeans(). In the first case, flights[2] is a data frame with a single column, so will have two dimensions with a row and column structure. In the second case, flights$month is a single numeric vector within the data frame and does not have a row and column dimension structure. If you used flights[[2]], that would be equivalent to flights$month, in referencing a single numeric vector. So, if you used: str(flights[2]) dim(flights[2]) str(flights$month) dim(flights$month) you would see the difference. Regards, Marc Schwartz> On Nov 4, 2020, at 8:26 AM, Engin Y?lmaz <ispanyolcom at gmail.com> wrote: > > Dear > I use *flights* database library(nycflights13) > > The following code is working as > > colMeans(flights[2]) > > * 6.54851* > > but other code is not working as > > colMeans(flights$month) > > *Error in colMeans(flights$month) : * > * 'x' must be an array of at least two dimensions* > > *flights[2]* is equal to the *month *column in database > > *Sincerely* > Engin YILMAZ
Hello, No, flights[2] is *not* equal to flights$months. The former is a data.frame with only one column, therefore it has a dimension attribute. The latter is a column, a vector of the data.frame flights, it does not have the attribute dim set. The difference is very important, see what class(), str() or dim() return when applied to both. See also this StackOverflow post: https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el Hope this helps, Rui Barradas ?s 13:26 de 04/11/20, Engin Y?lmaz escreveu:> Dear > I use *flights* database library(nycflights13) > > The following code is working as > > colMeans(flights[2]) > > * 6.54851* > > but other code is not working as > > colMeans(flights$month) > > *Error in colMeans(flights$month) : * > * 'x' must be an array of at least two dimensions* > > *flights[2]* is equal to the *month *column in database > > *Sincerely* > Engin YILMAZ > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >