dfs
dfs

Reputation: 29

create a vector with corresponding value

First of all, I had to create a vector with three objects. So, I did:

three.objects <- c('pen', 'pencil', 'notebook')

Then, I had to create a vector with cost of three objects that correspond to three objects.

cost <- c('pen' <- 5, 'pencil' <- 10, 'notebook' <- 5)

Is the second one correct way to create a vector with values in it?

Upvotes: 0

Views: 50

Answers (1)

akrun
akrun

Reputation: 887851

We can use setNames to create a named` vector

cost <- setNames(c(5, 10, 5), three.objects)
cost
#     pen   pencil notebook 
#       5       10        5 

Upvotes: 1

Related Questions