R 向量子集与列表子集
鉴于: 鉴于R 向量子集与列表子集,r,R,鉴于: 鉴于shining_list[[c(3,3)]]返回: actors_vector <- c("Jack Nicholson", "Shelley Duvall", "Danny Lloyd", "Scatman Crothers", "Barry Nelson") reviews_factor <- factor(c("Good", "OK", "Good", "Perfect",
shining_list[[c(3,3)]]
返回:
actors_vector <- c("Jack Nicholson", "Shelley Duvall", "Danny Lloyd",
"Scatman Crothers", "Barry Nelson")
reviews_factor <- factor(c("Good", "OK", "Good", "Perfect",
"Bad", "Perfect", "Good"),
levels = c("Bad", "OK", "Good", "Perfect"),
ordered = TRUE)
shining_list <- list(title = "The Shining",
actors = actors_vector,
reviews = reviews_factor)
shining_list
$title
[1] "The Shining"
$actors
[1] "Jack Nicholson" "Shelley Duvall" "Danny Lloyd" "Scatman Crothers"
[5] "Barry Nelson"
$reviews
[1] Good OK Good Perfect Bad Perfect Good
Levels: Bad < OK < Good < Perfect
$boxoffice
US Non-US
First release 39 47
Director's cut 18 14
[1] Good
Levels: Bad < OK < Good < Perfect
这是关于向量子集与列表子集的一节。这很可能是因为因子不是向量:
[1] 3
在某些情况下,此结构将崩溃,只剩下整数表示形式。我认为你的例子就是其中之一,其他几个例子是:
unclass(reviews)
## [1] 3 2 3 4 1 4 3
## attr(,"levels")
## [1] "Bad" "OK" "Good" "Perfect"
你在评论中有一个因素。当您使用
c(3,3)
进行提取时,它被转换为一个整数。它与[[可以递归地应用于列表相冲突,因此,如果单个索引i
是长度p
的向量,alist[[i]]
相当于alist[[i1]]
..[[ip]
在列表中提供除最终索引结果以外的所有结果。”,但是list[[3]]
返回[1]Good
/级别:Bad
而不仅仅是[1]3
。
unclass(reviews)
## [1] 3 2 3 4 1 4 3
## attr(,"levels")
## [1] "Bad" "OK" "Good" "Perfect"
c(reviews[3], reviews[4])
## [1] 3 4
ifelse(TRUE, reviews[1], reviews[2])
## [1] 3