Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...