user1471980
user1471980

Reputation: 10626

not able to read the columns in summary function

dput(x)
structure(c("2005-01-01:  1  ", "2005-01-02:  1  ", "2005-01-03:  1  ", 
"2005-01-04:  1  ", "2005-01-05:  1  ", "2005-01-06:  1  ", "(Other)   :724  ", 
"Min.   :  1.700  ", "1st Qu.:  3.062  ", "Median :  4.345  ", 
"Mean   :  6.267  ", "3rd Qu.:  7.435  ", "Max.   : 22.100  ", 
"NA's   :666.000  ", "Min.   :  0.2490  ", "1st Qu.:  0.6182  ", 
"Median :  1.0500  ", "Mean   :  2.2679  ", "3rd Qu.:  2.7825  ", 
"Max.   : 10.8000  ", "NA's   :666.0000  ", "Min.   :101  ", 
"1st Qu.:101  ", "Median :101  ", "Mean   :101  ", "3rd Qu.:101  ", 
"Max.   :101  ", NA), .Dim = c(7L, 4L), .Dimnames = list(c("", 
"", "", "", "", "", ""), c("Dat", "Var1", "Var2", "Name")), class = "table")

I am trying to look do this:

x$Var1 

and getting this error:

Error in x$Var1 : $ operator is invalid for atomic vectors

any idea, what I am doing is wrong here?

Upvotes: 0

Views: 283

Answers (3)

Matthew Lundberg
Matthew Lundberg

Reputation: 42629

This works:

> x[,'Var1']

"Min.   :  1.700  " "1st Qu.:  3.062  " "Median :  4.345  " "Mean   :  6.267  " "3rd Qu.:  7.435  " 

"Max.   : 22.100  " "NA's   :666.000  "

A simple example:

y <- rep(letters[1:3], each=10)
table(y)
## y
##  a  b  c 
## 10 10 10 

table(y)$a
## Error in table(y)$a : $ operator is invalid for atomic vectors

Upvotes: 2

IRTFM
IRTFM

Reputation: 263301

Matthew is giving you misleading advice rather than R being the entity which is misleading you. The error messages are accurate and if you pay attention to then you can learn valuable lessons. R tables are really matrices. They return TRUE from is.matrix and all the usual access methods work. The $ operator is for lists (of which 'dataframe' is a subclass ... but 'matrix' is not.) Notice that he offered you an access method that used the fact that matrices respond to access by named dimensional indices.

Upvotes: 0

thelatemail
thelatemail

Reputation: 93813

I am reading between the lines a little here, but to get the sort of data you want for each variable, you may be better off doing something like:

# create some test data
> test <- data.frame(Dat=1:10,Var1=1:10)
> result <- lapply(test,summary)
> result
$Dat
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    3.25    5.50    5.50    7.75   10.00 

$Var1
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    3.25    5.50    5.50    7.75   10.00 

Which would allow you to do:

> result$Var1
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    3.25    5.50    5.50    7.75   10.00 

Upvotes: 3

Related Questions