Assign Names to Vector Entries Without Assigning the Vector a Variable Name

Assign names to vector entries without assigning the vector a variable name?

How about using setNames(), which seems even cleaner/clearer than your suggested ideal?

z <- setNames(1:3, c("a", "b", "c"))
# z
# a b c
# 1 2 3

How to assign a variable values stored in a vector to a series of variable names stored in a character vector in R?

assign is not vectorized, so you can use Map here specifying the environment.

Map(function(x, y) assign(x, y, envir = .GlobalEnv), my_variables, my_values)

A
#[1] 1
B
#[1] 2
C
#[1] 3

However, it is not a good practice to have such variables in the global environment.

Use a named vector :

name_vec <- setNames(my_values, my_variables)
name_vec
#A B C
#1 2 3

Or named list as.list(name_vec).

Assign names to the vector elements of a list in R

You could pass the column names in sapply and assign names using setNames :

foo <- function(data, vars) sapply(vars, function(x) 
setNames(mean(data[[x]]) + c(-1, 1)*sd(data[[x]]),
paste0(x, '_', c('-', '+'), '1SD')), simplify = FALSE)

foo(data, c("pubs", "time"))

#$pubs
#pubs_-1SD pubs_+1SD
# 5.348203 33.909862

#$time
#time_-1SD time_+1SD
# 3.312931 12.848359

Assigning names to a vector retrieved by get()

You don't need get here at all:

object_names <- c('test1', 'test2', 'test3')

for (n in object_names) {
scores <- 1:5
names(scores) <- letters[1:5]
assign(paste0(n, '_scores'), scores)
assign(paste0(n, '_names'), names(scores))
}

test1_scores
#> a b c d e
#> 1 2 3 4 5

test1_names
#> [1] "a" "b" "c" "d" "e"

Note though, that if you have a bunch of similar objects with similar names, you will save yourself a lot of time and effort if you put your objects in lists rather than assigning them to the global environment:

object_names <- c('test1', 'test2', 'test3')

setNames(lapply(object_names, function(x) setNames(1:5, letters[1:5])),
paste0(object_names, "_score"))
#> $test1_score
#> a b c d e
#> 1 2 3 4 5
#>
#> $test2_score
#> a b c d e
#> 1 2 3 4 5
#>
#> $test3_score
#> a b c d e
#> 1 2 3 4 5

Created on 2022-06-20 by the reprex package (v2.0.1)

How add named element to R vector with name from a variable

Ronak Shah's answer worked well, but then I discovered an even simpler way:

V[x] <- 100

I'm going to post a new related and very similar question - How to define an R vector where some names are in variables.

R: How do I concisely assign names to vector parameter components?

Try:

v1 <- 1:4
names1 <- c("adam", "becky", "charlie", "david")

testfunction <- function(vect, names){
for(i in seq_along(names)){
assign(names[i], vect[i], envir=.GlobalEnv)
}
return(adam + becky^2 -2*charlie*david)
}

testfunction(v1, names1)
#[1] -19

Update

By changing .GlobalEnv to environment(), you can do the assign locally

testfunction <- function(vect, names){ 
for(i in seq_along(names1)){
assign(names1[i], vect[i],environment())
}
return(adam + becky^2 - 2*charlie*david)
}

testfunction(v1, names1)
#[1] -19
adam
#Error: object 'adam' not found

Assigning to objects using names created from a vector in R

We can use list2env on a named list

list2env(as.list(setNames(values, paste0("Obj", years))), envir = .GlobalEnv)

Obj1995
#[1] 1
Obj1996
#[1] 2

The assign can be used with a for loop

rm(list = ls(pattern = "^Obj\\d{4}$")) # remove any objects 
for(i in seq_along(values)) assign(paste0("Obj", yearsi]), value = values[i])

Is it possible to dynamically assign variable names to a vector in a magrittr pipeline?

With magrittr, set_names would be more appropriate (which is an Alias for `names<-`)

library(magrittr)
qnts %>%
quantile(dist, .) %>%
set_names(c("upper", "lower"))
# upper lower
# 0.75 2.25

Or can change the last step with setNames from base R instead of set_names

Another option would be to use names<-, but it would be less easier to understand

qnts %>%
quantile(dist, .) %>%
`names<-`(c("upper", "lower"))


Related Topics



Leave a reply



Submit