如果我连接一个数字( double ):
x <- c(0.5,0.6)
我似乎得到的是一个没有属性的对象,其类型为 double ,并且是一个 numeric 对象容器。
> x
[1] 0.5 0.6
> attributes(x)
NULL
> x
[1] 0.5 0.6
> type(x)
Error in type(x) : could not find function "type"
> typeof(x)
[1] "double"
> mode(x)
[1] "numeric"
我有点困惑,因为使用 c() 创建的容器与连接的对象具有相同的类型。我的理解是正确的还是根本错误的?
最佳答案
不确定我能否完全回答,但注意到 R 中没有标量,只有长度为 1 的向量可能会有所帮助。当您使用 c()
时,您可以连接两个或多个值制作更长的向量。
x <- 0
typeof(x)
#[1] "double"
length(x)
#[1] 1 # Now we have a double vector of length one.
y <- c(0)
typeof(y)
#[1] "double"
length(y)
#[1] 1 # Using c(), we made another double vector also of length one.
identical(x, y) # are x and y identical objects?
#[1] TRUE # yep!
z <- c(0, 0) # Now, use c() with two values
typeof(z)
#[1] "double"
length(z)
#[1] 2 # now we have a double vector of length two.
identical(x, z)
[1] FALSE
关于R c() 创建一个与连接的对象具有相同类的容器。正确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59516295/