The difference between 1:10 and c (1:10)

I understand that c is used to combine elements. But what is the difference between 1:10 and c (1:10)? I see that the outputs are the same. Should c (1:10) give an error, because 1:10 already unites all the elements?

> 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" 
+6
source share
1 answer

If you combine a function (aka c ) with only one parameter, this is the same as the identifier (otherwise it does not call the c function). Therefore, c(1:10) same as 1:10 . However, you can combine as many arguments as you want with another type (character, number ...). It will convert the type to you.

  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" 

Another difference is that you can call c with the recursive parameter. As the document says:

 ?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. 
+8
source

Source: https://habr.com/ru/post/979633/


All Articles