当前位置: 动力学知识库 > 问答 > 编程问答 >

r - Why does list() return a vector, and not a list?

问题描述:

Possible Duplicate:

why the object is vector?

Please see my code:

> x=function(z){z+1}

> y=list(n1=1,n2="qwe",n3=TRUE,n4=x)

> is.vector(y)

[1] TRUE

Why is y a vector? n1 is numeric, n2 is character, n3 is logical, n4 is a function.

They are different, so why is y a vector? Surely y can only be a list?

> data

name sex age height

1 x1 F 18 162

2 x2 M 19 170

3 x3 M 21 178

4 x4 F 22 166

5 x5 F 23 165

> data[1,]

name sex age height

1 x1 F 18 162

> is.vector(data[1,])

[1] FALSE

i am confused by vector ,why here data[1,] can not be a vectort?

网友答案:

You are using list which create a generic vector. Lists can contain different kind of objects, and are themselves vectors.

Thus is.vector gives the right answer. See here for further information.

Moreover if you type fix(y) you will see the structure:

structure(
 list(
  n1 = 1,
  n2 = "qwe",
  n3 = TRUE,
  n4 = function(z){z+1}
 ),
 .Names = c("n1", "n2", "n3", "n4")
)
网友答案:

y is a list:

> is.list(y)
[1] TRUE

You're confused because lists are vectors, which is described in the second paragraph of the Details section of ?is.vector. The same sentence says is.vector will also return TRUE for expressions:

> is.vector(as.expression(y))
[1] TRUE
分享给朋友:
您可能感兴趣的文章:
随机阅读: