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
366 views
in Technique[技术] by (71.8m points)

r - Why does is.vector() return TRUE for list?

I'm an R beginner. Browsing the R documentation, I stumbled upon this sentence ?is.vector: "If mode = "any", is.vector may return TRUE for the atomic modes, list and expression."

I'm just curious - why? All of the documentation I've read states that lists and vectors are two different data types. Is there some deeper R datatype concept I'm not getting?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

A list is (in most cases) itself a vector. From the help files for ?list: "Most lists in R internally are Generic Vectors, whereas traditional dotted pair lists (as in LISP) are available but rarely seen by users (except as formals of functions)."

This means you can use vector to pre-allocate memory for a list:

x <- vector("list", 3)
class(x)
[1] "list"

Now allocate a value to the second element in the list:

x[[2]] <- 1:5

x

[[1]]
NULL

[[2]]
[1] 1 2 3 4 5

[[3]]
NULL

See ?list and ?vector for more details.


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

...