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
任何想法,我在做什么都错了?
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分:0)
马修正在给你误导性建议,而不是R是误导你的实体。错误信息是准确的,如果您注意,那么您可以学习有价值的课程。 R表真的是矩阵。它们从is.matrix返回TRUE,所有常用的访问方法都有效。 $
运算符用于列表(其中'dataframe'是子类...但'matrix'不是。)请注意,他为您提供了一种访问方法,该方法使用了矩阵响应命名维度的访问这一事实指数。