Gordon, Fabiana
2016-Apr-27 14:25 UTC
[R] Create a new variable and concatenation inside a "for" loop
Hello, Suppose the you need a loop to create a new variable , i.e., you are not reading data from outside the loop. This is a simple example in Matlab code, for i=1:5 r1=randn r2=randn r=[r1 r2] c(i,:)=r; % creation of each row of c , % the ":" symbol indicates all columns. In R this would be [i,] end The output of interest is c which I'm creating inside the "for" loop -also the index used in the loop is used to create c. In R I had to create c as an empty vector (numeric() ) outside the loop, otherwise I get an error message saying that c doesn't exit. The other issue is the concatenation. In each iteration I'm creating the rows of c by placing the new row (r) below the previous one so that c becomes a 5 x 2 matrix. In R, it seems that I have no choice but use the function "rbind". I managed to write this code in R . However, I'm not sure that if instead of creating a new variable using the index in the "for" loop , I wanted to use the index to read data, e.g. suppose I have a 2 X 10 matrix X and suppose I want to calculate the sin () for each 2 x 2 sub-matrix of and stored in a matrix A. Then the code would be something like this, for i=1:5 A(:, 2*i-1:2*i)= sin(X(:, 2*i-1:2*i)) % the ":" symbol indicates all rows end Many Thanks, Fabiana Dr Fabiana Gordon Senior Statistical Consultant Statistical Advisory Service, School Of Public Health, Imperial College London 1st Floor, Stadium House, 68 Wood Lane, London W12 7RH. Tel: 020 7594 1749 Email: fabiana.gordon at imperial.ac.uk<mailto:fabiana.gordon at imperial.ac.uk> Web: www.imperial.ac.uk/research-and-innovation/support-for-staff/stats-advice-service/<http://www.imperial.ac.uk/research-and-innovation/support-for-staff/stats-advice-service/> [[alternative HTML version deleted]]
David L Carlson
2016-Apr-27 19:39 UTC
[R] Create a new variable and concatenation inside a "for" loop
Instead of talking about how you want to do something and showing us your Matlab code, tell us what you want to do, include reproducible data, and show us your R code so far. Your initial assumption, that you need a loop for these examples is incorrect if I have understood you correctly. Many functions in R are vectorized so that loops can often be avoided. To create a 5 x 2 matrix of random normal numbers with mean=0 and sd=1:> set.seed(42) > crand <- cbind(r1=rnorm(5), r2=rnorm(5)) # Don't use c as a variable since c() is a function > crandr1 r2 [1,] 1.3709584 -0.10612452 [2,] -0.5646982 1.51152200 [3,] 0.3631284 -0.09465904 [4,] 0.6328626 2.01842371 [5,] 0.4042683 -0.06271410 To compute sin() on pairs of rows and columns from a 2 x 10 matrix (this time we'll use uniform random numbers):> X <- matrix(runif(20), 2, 10) > X[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 0.9040314 0.9888917 0.08243756 0.3902035 0.4469696 0.7375956 0.3881083 0.003948339 [2,] 0.1387102 0.9466682 0.51421178 0.9057381 0.8360043 0.8110551 0.6851697 0.832916080 [,9] [,10] [1,] 0.007334147 0.9066014 [2,] 0.207658973 0.6117786> sin(X[, 1] - X[, 2])[1] -0.08475853 -0.72287775> t(sin(X[, 1:9] - X[, 2:10]))[,1] [,2] [1,] -0.084758528 -0.72287775 [2,] 0.787322543 0.41910237 [3,] -0.302930277 -0.38159970 [4,] -0.056735679 0.06967737 [5,] -0.286552020 0.02494653 [6,] 0.342416179 0.12555319 [7,] 0.374780442 -0.14720941 [8,] -0.003385802 0.58530576 [9,] -0.782871221 -0.39320949 ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Gordon, Fabiana Sent: Wednesday, April 27, 2016 9:25 AM To: 'r-help at R-project.org' Subject: [R] Create a new variable and concatenation inside a "for" loop Hello, Suppose the you need a loop to create a new variable , i.e., you are not reading data from outside the loop. This is a simple example in Matlab code, for i=1:5 r1=randn r2=randn r=[r1 r2] c(i,:)=r; % creation of each row of c , % the ":" symbol indicates all columns. In R this would be [i,] end The output of interest is c which I'm creating inside the "for" loop -also the index used in the loop is used to create c. In R I had to create c as an empty vector (numeric() ) outside the loop, otherwise I get an error message saying that c doesn't exit. The other issue is the concatenation. In each iteration I'm creating the rows of c by placing the new row (r) below the previous one so that c becomes a 5 x 2 matrix. In R, it seems that I have no choice but use the function "rbind". I managed to write this code in R . However, I'm not sure that if instead of creating a new variable using the index in the "for" loop , I wanted to use the index to read data, e.g. suppose I have a 2 X 10 matrix X and suppose I want to calculate the sin () for each 2 x 2 sub-matrix of and stored in a matrix A. Then the code would be something like this, for i=1:5 A(:, 2*i-1:2*i)= sin(X(:, 2*i-1:2*i)) % the ":" symbol indicates all rows end Many Thanks, Fabiana Dr Fabiana Gordon Senior Statistical Consultant Statistical Advisory Service, School Of Public Health, Imperial College London 1st Floor, Stadium House, 68 Wood Lane, London W12 7RH. Tel: 020 7594 1749 Email: fabiana.gordon at imperial.ac.uk<mailto:fabiana.gordon at imperial.ac.uk> Web: www.imperial.ac.uk/research-and-innovation/support-for-staff/stats-advice-service/<http://www.imperial.ac.uk/research-and-innovation/support-for-staff/stats-advice-service/> [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jeff Newmiller
2016-Apr-27 19:57 UTC
[R] Create a new variable and concatenation inside a "for" loop
"c" an extremely commonly-used function. Functions are first-class objects that occupy the same namespaces that variables do, so they can obscure each other. In short, don't use variables called "c" (R is case sensitive, so "C" has no such problem). Wherever possible, avoid incremental concatenation like the plague. If you feel you must use it, at least concatenate in lists and then use functions like unlist, do.call, or pre-allocate vectors or matrix-like objects with unuseful values like NA and then overwrite each element in the vector or matrix-type object in a loop like your first one. -- Sent from my phone. Please excuse my brevity. On April 27, 2016 3:25:14 PM GMT+01:00, "Gordon, Fabiana" <fabiana.gordon at imperial.ac.uk> wrote:>Hello, > >Suppose the you need a loop to create a new variable , i.e., you are >not reading data from outside the loop. This is a simple example in >Matlab code, > >for i=1:5 >r1=randn >r2=randn >r=[r1 r2] >c(i,:)=r; % creation of each row of c , % the ":" symbol indicates >all columns. In R this would be [i,] >end > >The output of interest is c which I'm creating inside the "for" loop >-also the index used in the loop is used to create c. In R I had to >create c as an empty vector (numeric() ) outside the loop, otherwise I >get an error message saying that c doesn't exit. > >The other issue is the concatenation. In each iteration I'm creating >the rows of c by placing the new row (r) below the previous one so >that c becomes a 5 x 2 matrix. >In R, it seems that I have no choice but use the function "rbind". I >managed to write this code in R . However, I'm not sure that if instead >of creating a new variable using the index in the "for" loop , I >wanted to use the index to read data, e.g. suppose I have a 2 X 10 >matrix X and suppose I want to calculate the sin () for each 2 x 2 >sub-matrix of and stored in a matrix A. Then the code would be >something like this, > >for i=1:5 >A(:, 2*i-1:2*i)= sin(X(:, 2*i-1:2*i)) % the ":" symbol indicates all >rows >end > >Many Thanks, > >Fabiana > > >Dr Fabiana Gordon > >Senior Statistical Consultant >Statistical Advisory Service, School Of Public Health, >Imperial College London >1st Floor, Stadium House, 68 Wood Lane, >London W12 7RH. > >Tel: 020 7594 1749 >Email: >fabiana.gordon at imperial.ac.uk<mailto:fabiana.gordon at imperial.ac.uk> >Web: >www.imperial.ac.uk/research-and-innovation/support-for-staff/stats-advice-service/<http://www.imperial.ac.uk/research-and-innovation/support-for-staff/stats-advice-service/> > > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]
Bert Gunter
2016-Apr-27 20:18 UTC
[R] Create a new variable and concatenation inside a "for" loop
... "(R is case sensitive, so "C" has no such problem)." Well, not quite. Try ?C To add to the previous comments, Dr. Gordon appears to need to do her/his homework and spend some time with an R tutorial or two before posting further here. There are many good ones on the web. Some recommendations can be found here: https://www.rstudio.com/online-learning/#R Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Wed, Apr 27, 2016 at 12:57 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> "c" an extremely commonly-used function. Functions are first-class objects that occupy the same namespaces that variables do, so they can obscure each other. In short, don't use variables called "c" (R is case sensitive, so "C" has no such problem). > > Wherever possible, avoid incremental concatenation like the plague. If you feel you must use it, at least concatenate in lists and then use functions like unlist, do.call, or pre-allocate vectors or matrix-like objects with unuseful values like NA and then overwrite each element in the vector or matrix-type object in a loop like your first one. > -- > Sent from my phone. Please excuse my brevity. > > On April 27, 2016 3:25:14 PM GMT+01:00, "Gordon, Fabiana" <fabiana.gordon at imperial.ac.uk> wrote: >>Hello, >> >>Suppose the you need a loop to create a new variable , i.e., you are >>not reading data from outside the loop. This is a simple example in >>Matlab code, >> >>for i=1:5 >>r1=randn >>r2=randn >>r=[r1 r2] >>c(i,:)=r; % creation of each row of c , % the ":" symbol indicates >>all columns. In R this would be [i,] >>end >> >>The output of interest is c which I'm creating inside the "for" loop >>-also the index used in the loop is used to create c. In R I had to >>create c as an empty vector (numeric() ) outside the loop, otherwise I >>get an error message saying that c doesn't exit. >> >>The other issue is the concatenation. In each iteration I'm creating >>the rows of c by placing the new row (r) below the previous one so >>that c becomes a 5 x 2 matrix. >>In R, it seems that I have no choice but use the function "rbind". I >>managed to write this code in R . However, I'm not sure that if instead >>of creating a new variable using the index in the "for" loop , I >>wanted to use the index to read data, e.g. suppose I have a 2 X 10 >>matrix X and suppose I want to calculate the sin () for each 2 x 2 >>sub-matrix of and stored in a matrix A. Then the code would be >>something like this, >> >>for i=1:5 >>A(:, 2*i-1:2*i)= sin(X(:, 2*i-1:2*i)) % the ":" symbol indicates all >>rows >>end >> >>Many Thanks, >> >>Fabiana >> >> >>Dr Fabiana Gordon >> >>Senior Statistical Consultant >>Statistical Advisory Service, School Of Public Health, >>Imperial College London >>1st Floor, Stadium House, 68 Wood Lane, >>London W12 7RH. >> >>Tel: 020 7594 1749 >>Email: >>fabiana.gordon at imperial.ac.uk<mailto:fabiana.gordon at imperial.ac.uk> >>Web: >>www.imperial.ac.uk/research-and-innovation/support-for-staff/stats-advice-service/<http://www.imperial.ac.uk/research-and-innovation/support-for-staff/stats-advice-service/> >> >> >> >> [[alternative HTML version deleted]] >> >>______________________________________________ >>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>https://stat.ethz.ch/mailman/listinfo/r-help >>PLEASE do read the posting guide >>http://www.R-project.org/posting-guide.html >>and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.