0

I'm working with a dataframe with 16 colums right? I want to calculate the mean of the values in one of them,

The funny thing is, it only works when I call it using $

 mean(datospractica$REGISTRO)

[1] 2202

but when I call, the exact same colum using [] I get these.

mean(datospractica[1])

[1] NA Warning message: In mean.default(datospractica[1]) : argument is not numeric or logical: returning NA

so, wtf? is it not that $ should be equivalent to [] ? Why does it not work in the second case?

it also does not work like this

 mean(datospractica["REGISTRO"])

[1] NA Warning message: In mean.default(datospractica["REGISTRO"]) : argument is not numeric or logical: returning NA

I have double checked, it is the same column, so, why does this happen?

Elin
  • 6,507
  • 3
  • 25
  • 47
brandata
  • 81
  • 9
  • 4
    `datospractica[[1]]` or `datospractica[["REGISTRO"]]`you have to dive into the list (a dataframe is a special kind of list). – jogo Nov 26 '18 at 11:33
  • 2
    or `datospractica[, "REGISTRO"]` / `datospractica[, 1]` – Cath Nov 26 '18 at 11:34
  • 4
    Related / possible duplicate: [*`The difference between bracket [] and double bracket [[]] for accessing the elements of a list or dataframe`*](https://stackoverflow.com/q/1169456/2204410) – Jaap Nov 26 '18 at 11:44

1 Answers1

0

It is because data.frames are esentially lists of vectors of the same length and subsetting lists by single brackets x[] always returns a list. The mean of that is not defined and so it does not work. Instead, you should use the double brackets x[[1]] or single bracket x[,1] subsetting fuctions.

Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37