5
考慮:矢量子集與列表子集
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
爲什麼shining_list[[3]][3]
和shining_list$reviews[3]
回報:
[1] Good
Levels: Bad < OK < Good < Perfect
鑑於shining_list[[c(3,3)]]
回報:
[1] 3
這是一款在矢量子集設置與列表子集設置在DataCamp。
你在評論中有一個因素。當你用'c(3,3)'進行提取時,它會被轉換爲一個整數。 –
儘管OP在問_why_。它與_「'[[''可以遞歸地應用於列表,因此如果單個索引'i'是長度爲'p'的向量,'alist [[i]]'等同於'alist [[i1 ]]''...'[[ip]]'除了最終的索引結果外,都提供一個列表。「,但是'shining_list [[3]] [[3]]'returns'[1]好的'''evels :壞<好<好<完美<,不只是'[1] 3'。 – hrbrmstr