为什么seq()创建int和num向量而c()从不创建int向量?

str*_*agu 2 r

我无法弄清楚为什么seq()可以输出不同的类,具体取决于元素中是否存在小数点,而c()无论是否存在小数,总是会创建一个num向量.

例如:

seqDec <- seq(1, 2, 0.5) # num vector
cDec <- c(1, 1.5, 2) # num vector
seqInt <- seq(1, 5) # int vector
cInt <- c(1, 2, 3, 4, 5) # num vector
Run Code Online (Sandbox Code Playgroud)

Kon*_*lph 5

c通过连接您提供的元素来创建矢量.相反,seq实际上通过基于特定参数生成新数字来构造序列.

因此c(1, 2, 4, 5),在参数的情况下,numeric结果class(1)也是如此(是numeric,不是integer!).您可以integer通过提供整数生成向量:

intvec = c(1L, 2L, 3L, 4L, 5L)
Run Code Online (Sandbox Code Playgroud)

繁琐.但可能.

seq,如果不提供一个步长大小(by参数),该函数默认为生成的整数值; 在这方面,它相当于:运营商:

intvec = 1 : 5
Run Code Online (Sandbox Code Playgroud)

实际上,如果你没有提供bylength.out参数,那么在seq(from, to)内部执行from : to.