Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Used official product name, code formatted variable name
Source Link
Adriaan
  • 18.2k
  • 7
  • 47
  • 88

I can make this code:

#set seed set.seed(848) #make variables (similar, not same) myvar_a <- rnorm(n = 100, mean = 1, sd = 2) myvar_b <- rnorm(n = 100, mean = 2, sd = sqrt(3)) myvar_c <- rnorm(n = 100, mean = 4, sd = sqrt(5)) myvar_d <- rnorm(n = 100, mean = 8, sd = sqrt(8)) #transform variables for(i in 1:4){ if(i ==1){ myvar_1 <- myvar_a } else if (i==2) { myvar_2 <- myvar_b } else if (i==3) { myvar_3 <- myvar_b } else { myvar_4 <- myvar_b } } 

It gives me this:

enter image description here

Is there a way to do it with "paste" and the loop variable?

In MatLabMATLAB there is the "eval"eval that treats a constructed character string as a line of code, so I could create sentences then run them from within the code.

I can make this code:

#set seed set.seed(848) #make variables (similar, not same) myvar_a <- rnorm(n = 100, mean = 1, sd = 2) myvar_b <- rnorm(n = 100, mean = 2, sd = sqrt(3)) myvar_c <- rnorm(n = 100, mean = 4, sd = sqrt(5)) myvar_d <- rnorm(n = 100, mean = 8, sd = sqrt(8)) #transform variables for(i in 1:4){ if(i ==1){ myvar_1 <- myvar_a } else if (i==2) { myvar_2 <- myvar_b } else if (i==3) { myvar_3 <- myvar_b } else { myvar_4 <- myvar_b } } 

It gives me this:

enter image description here

Is there a way to do it with "paste" and the loop variable?

In MatLab there is the "eval" that treats a constructed character string as a line of code, so I could create sentences then run them from within the code.

I can make this code:

#set seed set.seed(848) #make variables (similar, not same) myvar_a <- rnorm(n = 100, mean = 1, sd = 2) myvar_b <- rnorm(n = 100, mean = 2, sd = sqrt(3)) myvar_c <- rnorm(n = 100, mean = 4, sd = sqrt(5)) myvar_d <- rnorm(n = 100, mean = 8, sd = sqrt(8)) #transform variables for(i in 1:4){ if(i ==1){ myvar_1 <- myvar_a } else if (i==2) { myvar_2 <- myvar_b } else if (i==3) { myvar_3 <- myvar_b } else { myvar_4 <- myvar_b } } 

It gives me this:

enter image description here

Is there a way to do it with "paste" and the loop variable?

In MATLAB there is the eval that treats a constructed character string as a line of code, so I could create sentences then run them from within the code.

Source Link
EngrStudent
  • 2k
  • 1
  • 32
  • 53

how to rename a variable in r, using a composed name

I can make this code:

#set seed set.seed(848) #make variables (similar, not same) myvar_a <- rnorm(n = 100, mean = 1, sd = 2) myvar_b <- rnorm(n = 100, mean = 2, sd = sqrt(3)) myvar_c <- rnorm(n = 100, mean = 4, sd = sqrt(5)) myvar_d <- rnorm(n = 100, mean = 8, sd = sqrt(8)) #transform variables for(i in 1:4){ if(i ==1){ myvar_1 <- myvar_a } else if (i==2) { myvar_2 <- myvar_b } else if (i==3) { myvar_3 <- myvar_b } else { myvar_4 <- myvar_b } } 

It gives me this:

enter image description here

Is there a way to do it with "paste" and the loop variable?

In MatLab there is the "eval" that treats a constructed character string as a line of code, so I could create sentences then run them from within the code.