-2

I want to assign 100 variables to 100 values. How can I do this?

For example if I want x1, x2, x3, .... x100 to be assigned with 100 values at once (say x1 = 5, x2 = "c143", x3 = 0, ....)

What is the way to do this? Please advise, didn't find a way to do this here/Dr. G. I have tried lapply/sapply/for loops but maybe there is more easy straight forward way?

steves
  • 331
  • 3
  • 16
  • can you please provide a [minimal reporducible](https://stackoverflow.com/help/mcve) example. also have a look at [how to write a good R question](https://stackoverflow.com/q/5963269/3250126). – loki May 08 '18 at 14:29
  • Not very clear what you mean maybe something like `setNames(c('A', 'B', 'C'), paste0('x', seq(3)))`? – Sotos May 08 '18 at 14:30
  • I have shown you what I want: you have 100 variables: x1 to x100. You want to assign all of them with 100 values. – steves May 08 '18 at 14:30
  • Further, you are probably looking for a `join` of some kind – loki May 08 '18 at 14:31
  • Re: *I have tried lapply/sapply/for loops but maybe there is more easy straight forward way?* Can you please share? – Sotos May 08 '18 at 14:31
  • @Sotos you mean that for simplicity if I have x1, x2, x3 and use your method I will be able to assign them all at once to 1,2,3 for example? – steves May 08 '18 at 14:31
  • @Sotos I think you are the closest. Will try it. I thought I can just do something like c(x1, x2, ..., x100) <- (1, ... , 100) – steves May 08 '18 at 14:33
  • If you share what you have tried with your `lapply` etc... so I can see the structure of your output I'd be able to help you further. – Sotos May 08 '18 at 14:36
  • @Sotos What I actualy wanted to do is to assign a vector of variable names to values. Your solution seems to be the best. – steves May 08 '18 at 14:38
  • 1
    I could be that you mean something like this: `l <- list(x1 = 5, x2 = "dfs"); list2env(l, envir = globalenv())`. – r.user.05apr May 08 '18 at 14:39
  • 1
    If `a <- 1:100`, you can do `names(a) <- paste0('x', 1:100)` This will give you a named integer vector, but you won't be able to use that with easy indexing. If you want to be able to do fx `a$52`afterwards, use: `a <- as.list(1:100)` first. – Majo May 08 '18 at 14:42

2 Answers2

2

You can use setNames, i.e.

setNames(c('A', 'B', 'C'), paste0('x', seq(3)))
# x1  x2  x3 
#"A" "B" "C"
Sotos
  • 51,121
  • 6
  • 32
  • 66
1

This really depends on what you are trying to achieve. If you have 100 different names for variables stored as character strings and numeric values stored in a numeric vector, then you can do the following:

# vector with names for variables
vars <- c("x1","x2","x3")

# vector with values for variables
vals <- c( 1,2,3)


#check variable list
ls()
# [1] "vals" "vars"

# assign values to variables
for (i in seq(vars)) assign(vars[i],vals[i])

# check variable list
ls()
# [1] "i"    "vals" "vars" "x1"   "x2"   "x3"

As suggested in comments there are some other approaches (again depending on how you want to use the variable names): Assign multiple new variables on LHS in a single line in R

Katia
  • 3,784
  • 1
  • 14
  • 27