Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

r - Why does the use of [ versus [[ sometimes produce the SAME result (rather than different results)?

The R documentation (as well as extensive R folklore) describes the different operators for extraction.

However, it appears that neither the documentation nor the folklore explain why it is that the use of different operators ( [ versus [[ ) can return identical results. Can someone provide an explanation? Note that I'm asking not why they can produce different results, but why they can sometimes produce an identical result. Why does x[2] (see below) evaluate identically to x[[2]]?

x <- c("a","b","c") ; x[2] ; x[[2]]
## [1] "b"
## [1] "b"
question from:https://stackoverflow.com/questions/65646401/why-does-the-use-of-versus-sometimes-produce-the-same-result-rather-than-d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is no distinction in R between a value (e.g. 3) and a vector of length 1 (e.g. c(3)). x[[2]] should give you a value and x[2] should give you a vector of length 1. But these two things are identical.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...