JustPeter
JustPeter

Reputation: 150

What is the difference between creating a vector with c() and as.vector?

I would like to create a vector, numeric vector to be specific in R Studio. I met these two while reading some matters about R. I am quite confused about the difference between the two. Which should I use or which is better?

 mydata <- c(1,2,3,4,5)

and

 mydata <- as.vector(c(1,2,3,4,5))

Upvotes: 1

Views: 1637

Answers (3)

mhovd
mhovd

Reputation: 4077

There is no need to call as.vector, as c() will create vector with the given entries. as.vector(c(1,2,3)) would try to coerce a vector to a vector, and will do nothing in this case.

You can see that both have the same type with str(), e.g.

> str(c(1,2,3))
 num [1:3] 1 2 3
> str(as.vector(c(1,2,3)))
 num [1:3] 1 2 3

Both will evaluate to a numeric vector.

The manual might be able to explain some more. You can look up help text for (most) functions by prepending a ? to the function name.

?c

This is a generic function which combines its arguments.

The default method combines its arguments to form a vector. All arguments are coerced to a common type which is the type of the returned value, and all attributes except names are removed.

The type of the vector will depend on the input you are giving it. If you are providing c() with numeric values, it will be a numeric vector, and the same for strings. Note that mixing types in vectors may have undesired consequences, as a vector can only have one type.

?as.vector

as.vector, a generic, attempts to coerce its argument into a vector of mode mode (the default is to coerce to whichever vector mode is most convenient): if the result is atomic all attributes are removed.

Notice that I wrote "in this case" for as.vector(c(1,2,3)). If you had a named vector calling as.vector() would remove the names, e.g.

> vec = c("abc" = 2, "def" = 3)
> vec
abc def 
  2   3 
> as.vector(vec)
[1] 2 3

Notice that the names are removed.

Upvotes: 3

Rui Barradas
Rui Barradas

Reputation: 76565

The main difference is in the attribute names.

From the help pages help('as.vector'), my emphasis:

as.vector, a generic, attempts to coerce its argument into a vector of mode mode (the default is to coerce to whichever vector mode is most convenient): if the result is atomic all attributes are removed.

and help('c'), my emphasis:

The default method combines its arguments to form a vector. All arguments are coerced to a common type which is the type of the returned value, and all attributes except names are removed.

mydata <- setNames(c(1,2,3,4,5), letters[1:5])

c(mydata)
#a b c d e 
#1 2 3 4 5 

as.vector(mydata)
#[1] 1 2 3 4 5

Upvotes: 0

Sirius
Sirius

Reputation: 5429

Inspect the result with dput if in doubt:

dput( c(1,2,3,4,5) )
dput( as.vector(c(1,2,3,4,5) ))

It gives you the R-code necessary to recreate them. They should come out identical.

as.vector turns it's first argument into a vector, and offers additional functionality with the modeargument. c combines multiple arguments, coercing them to a common type. The manuals have examples and further documetnation ?c and ?as.vector

Upvotes: 0

Related Questions