0
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")

我正在尝试这样做:

x$Var1 

并收到此错误:

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

任何想法,我在这里做错了吗?

4

3 回答 3

3

我在这里读了一些字,但是要为每个变量获取所需的数据类型,最好执行以下操作:

# 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 

这将允许您执行以下操作:

> result$Var1
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    3.25    5.50    5.50    7.75   10.00 
于 2013-01-15T03:28:18.053 回答
2

这有效:

> 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  "

一个简单的例子:

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
于 2013-01-15T03:03:33.457 回答
0

马修是给你误导性的建议,而不是 R 是误导你的实体。错误消息是准确的,如果您注意,那么您可以学到宝贵的经验。R 表实际上是矩阵。它们从 is.matrix 返回 TRUE 并且所有常用的访问方法都有效。该$运算符用于列表(其中“数据框”是子类......但“矩阵”不是。)请注意,他为您提供了一种访问方法,该方法使用矩阵响应命名维度索引的访问这一事实。

于 2013-01-15T04:49:05.873 回答