Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 84
Further information – Double square brackets
ОглавлениеThe double square brackets will inform R that we want one element returned as the most elementary class and is limited to returning one position. The simple square brackets will return a list and can be given a range.
# The first object of L2 as a list: L2[1] ## [[1]] ## [1] 1 2 3 class(L[2]) ## [1] “list” # The first element of L2 is a numeric vector: L2[[1]] ## [1] 1 2 3 class(L2[[2]]) ## [1] “integer” # range L2[1:2] ## [[1]] ## [1] 1 2 3 ## ## [[2]] ## [1] 2 3 4 5 6 7 # Unexpected result (ranges are not to be used with x[[.]]): L2[[1:2]] <- ‘a’ L2 ## [[1]] ## [1] “1” “a” “3” ## ## [[2]] ## [1] 2 3 4 5 6 7 # Is this what you would expect? L2[1:2] <- ‘a’ L2 ## [[1]] ## [1] “a” ## ## [[2]] ## [1] “a”