我知道c用于组合元素.但1:10和c(1:10)之间有什么区别?我看到输出是一样的.不应该c(1:10)给出错误,因为1:10已经结合了所有元素?
> 1:10
[1] 1 2 3 4 5 6 7 8 9 10
> c(1:10)
[1] 1 2 3 4 5 6 7 8 9 10
> class(1:10)
[1] "integer"
> class(c(1:10))
[1] "integer"
Run Code Online (Sandbox Code Playgroud)
如果将(aka c函数)与仅一个参数组合,则它与标识相同(也就是不调用c函数).因此c(1:10)是一样的1:10.但是,您可以根据需要使用不同类型(字符,数字......)组合多个参数.它会为你转换类型.
all.equal(1:10,c(1:5,6:10))
[1] TRUE
all.equal("meow",c("meow"))
[1] TRUE
c(1:5,6:10,"meow")
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "meow"
class(c(1:5,6:10,"meow"))
[1] "character"
Run Code Online (Sandbox Code Playgroud)
另一个区别是你可以使用参数recursive调用c.正如文件所述:
?c
Usage
c(..., recursive = FALSE)
Arguments
...
objects to be concatenated.
recursive
logical. If recursive = TRUE, the function recursively descends through lists (and pairlists) combining all their elements into a vector.
Run Code Online (Sandbox Code Playgroud)