Name Variables in for-Loop Dynamically in R (2 Examples) | Create New Variable with assign Function

preview_player
Показать описание
R code of this video:

my_list <- list(1:5, # Create list in R
"AAA",
9:5)
my_list # Print list to console

assign("variable_1", my_list[[1]]) # Apply assign function
variable_1

for(i in 1:length(my_list)) { # assign function within loop
assign(paste0("variable_", i), my_list[[i]])
}

variable_1 # Print variables
variable_2
variable_3
Рекомендации по теме
Комментарии
Автор

This channel is extremely underrated! Thank you very much !!!

NamTran-jdlp
Автор

I'm so gratefull about this video! Thank's

DanielAraujo
Автор

Your videos are great for discovering more about the applications of "for" loops. Thank you!!

agsoutas
Автор

This was massively helpful, thank you very much!

JoelSilva-bszz
Автор

Exactly what I was looking for !!Thanks bro..great videos too✌️

GaMiNGYT-dccf
Автор

As always, very useful video. Thank you!

forrestoakley
Автор

Thank you. This is a very helpful video. I have a follow up question. Is it possible to now iterate through these list of variables and perform a specific list of actions on each of these variables like in a for loop?

thironthoramdude
Автор

Hi - thanks for this, really helpful. I have a query though; if I was to create data frames using this method, and then wanted to add additional columns to said data frames, how would I reference the data frames in the code?

sambull
Автор

Is it possible to instead of the second part in the body of the for loop, to use an operation which substracts two other lists with i elements
like: for(i in 1:length(D_year_cumprod)) {
assign(paste0("ssd_jahr_", i),
(C_year_cumprod[[i]] - D_year_cumprod[[i]])
}
(my code doesnt work...)?

franz
Автор

Hallo Joachim. Wie immer ein super Video! :-)

Ich habe hierzu aber noch eine Frage. Besteht die Möglichkeit, dass ich das Ganze auch für die einzelen Werte innerhalb einer Variable für mehrere ID's machen kann?
Also ich habe bspw. die Variable "dt" mit den Werten 4/1/20 für Monat, Tag und Jahr bis z.B. 4/30/20 und dies jeweils für alle 24 Personen. Was ich nun machen will, ist, dass ich jede Zeitangabe der Form "4/1/20 bis 4/30/20" jeweils aufsteigend mit den Werten 1 bis 30 anhand einer for-schleife ersetzen möchte. Ich vermute, dass hierfür die replace-funktion angewendet wird, weiß aber nicht, wie ich die Zeitangaben mit 1-30 ersetzen kann. Wenn Du mir da weiterhelfen könntest, wäre ich Dir extrem dankbar.

Viele Grüße

nolevel
Автор

refering to my other question: If i dont want to assign an new xts object for each iteration, which i afterwards have to merge.
how do i save my solutioncoulmn for each iteration in one xts with colnames refering to the iteration i´m doing?
like :
for (i in 1: length(topshares)
{calculation gives xts object spread_jahr with many columns
and new xts object pfreturn should save the column spread_jahr$portfoilio_return for each iteration in it, but create a new column and don´t overwrite the col from last iteration
pfreturn$[i] <- spread_jahr$portfoilio_return
}
the result should be like this:
1. column: name [i]
2. column : name[i]

Do you have an idea what is missing on my code?

franz
Автор

I have a vector with 605 names that i want put on my variables, basically in the end of the process i need 605 variables.
So can i replace the "list" by the vector with the names? Like this example, with 3 names:

my_list <- c("A80226",
"A82547",
"A89654")

for(i in 1:length(my_list)) {
assign(paste0("variable_", my_list[i]), my_list[i])
}

DanielAraujo
Автор

Is there a way to call the variable with the loop variable --> variable_{$i}. Being $i the number within the loop seq. I have a ggplot scirpt and different groups (1, 2, 3..) and i want to alocate the ggplot script within a loop to make one plot for each group --> ggplot(group_{i}). Thank you !!!

aitorvega
Автор

Kindly tell me.. How can I recode categorical variables using for loop

poojamahesh