How to Create Dynamic Variable Names Inside a Loop

How do I create dynamic variable names inside a loop?

Use an array for this.

var markers = [];
for (var i = 0; i < coords.length; ++i) {
markers[i] = "some stuff";
}

How do you create different variable names while in a loop?

Sure you can; it's called a dictionary:

d = {}
for x in range(1, 10):
d["string{0}".format(x)] = "Hello"
>>> d["string5"]
'Hello'
>>> d
{'string1': 'Hello',
'string2': 'Hello',
'string3': 'Hello',
'string4': 'Hello',
'string5': 'Hello',
'string6': 'Hello',
'string7': 'Hello',
'string8': 'Hello',
'string9': 'Hello'}

I said this somewhat tongue in check, but really the best way to associate one value with another value is a dictionary. That is what it was designed for!

Dynamic variable names to mutate variables in for-loop

As we are passing string, convert to symbol and evaluate (!!)

func <- function(i) {

mutate(df1, !!i := case_when(!is.na(!! rlang::ensym(i)) ~ as.character(!! rlang::ensym(i)),
is.na(!!rlang::ensym(i)) & var0 != '1' ~ '4444',
TRUE ~ '0'))
}

-testing

for(i in vars) {
df1 <- func(i)
}
df1
var0 var1 var2 var3
1 1 0 1 NA
2 2 1 4444 1
3 2 0 0 0
4 1 1 0 4444
5 1 0 1 1
6 2 4444 4444 NA
7 2 4444 4444 1

We may do this with across as well

df1 %>%
mutate(across(all_of(vars),
~ case_when(!is.na(.) ~ as.character(.),
is.na(.) & var0 != '1' ~ '4444', TRUE ~ '0')))
var0 var1 var2 var3
1 1 0 1 NA
2 2 1 4444 1
3 2 0 0 0
4 1 1 0 4444
5 1 0 1 1
6 2 4444 4444 NA
7 2 4444 4444 1

JavaScript: Dynamically Creating Variables for Loops

You should use an array:

function createVariables(){
var accounts = [];

for (var i = 0; i <= 20; ++i) {
accounts[i] = "whatever";
}

return accounts;
}

You then have access to accounts[0] through accounts[20].

Dynamic variable in loop javascript

You don't want to create 9 variables. Trust me. You want to create an object.

var m = {};
for(var i=1; i<10; i++){
m[i] = "Something";
}

You can also create an array (m = []), but since you are starting at 1 and not 0, I'd suggest an object.

R: Read dynamic variable names generated in for loop

You may use get to do this -

library(dplyr)

for (i in 1:nrow(LabviewFiles)){
assign(x = paste("P",i , "Onsets_PRH", sep = "_"), value = t(subset.data.frame(All_Phase, All_Phase$Phase==i) %>%
filter(CONDITIONS == "NULL_TRIAL",
MISC_REWARD == 1,
MISC_PASSIVE_FAILED == 1) %>%
select(Feedback_onset)))

assign(x = paste("P",i , "Durations_PRH", sep = "_"), value = t(rep(0.5, times = length(get(paste("P",i , "Onsets_PRH", sep = "_"))))))
}

Note that using assign and creating variables in global environment is discouraged. You may also read Why is using assign bad?

How to create variable names with a for loop?

Creating variables dynamically is an anti-pattern and should be avoided. What you need is actually a list:

total_squares = 8
box_list = []
boxes = [0] * total_squares
for q in range(total_squares):
box_list.append(boxes[q])

Then you can refer to any element you want (say, box_i) using the following syntax:

my_box = box_list[boxes[i]]


Related Topics



Leave a reply



Submit