Sorry if these questions have been asked recently--I'm new to this list. I'm primarily a Matlab user who is attempting to learn R and I'm searching for possible equivalents of commands that I found very handy in Matlab. So that I don't seem ungrateful to those who may answer, I HAVE determined ways to carry out these processes in 'brute force' sorts of ways in R code, but they lack the elegance and simplicity of the Matlab commands. Also, if you know that no such commands exist, that bit of knowledge would be helpful to know so that I don't continue fruitless searches. The first is Matlab's 'find' command. This is one of the most useful commands in Matab. Basically, if X is the vector X=[3, 2, 1, 1, 2, 3] the command 'find(X==1)' would return the vector [3, 4] which would indicate that the vector X had the value of 1 at the 3 and 4 positions. This was an extremely useful command for subsetting in Matlab. The closest thing I've found in R has been 'match' but match only returns the first value as opposed to the position of all matching values. The second Matlab command that I'd like to find an R equivalent for is 'end'. 'end' is just a simple little command that indicates the end of a row/column. It is incredibly handy when used to subset matrices like Y = X(2:end) and produces Y=[2, 1, 1, 2, 3] if the X is the same as in the previous example. This cutsie little command was extremely useful for composing programs that were flexible and could use input matrices of any size without modifying the code. I realize that you can accomplish the same by Y <- X[2:length(X)] in R, but this method is ungainly, particularly when subsetting matrices rather than vectors. If anyone has advice, I'd be grateful, Bryan L. Brown Integrative Biology University of Texas at Austin Austin, TX 78712 512-965-0678 stonefly@mail.utexas.edu [[alternative HTML version deleted]]
Hi Bryan, 1. which(x==1) 2. X[2:nrow(X),] or X[,2:ncol(X)] The "An Introduction to R" document is very usdeful for this kind of things. I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Bryan L. Brown" <stonefly at mail.utexas.edu> To: <R-help at stat.math.ethz.ch> Sent: Thursday, October 07, 2004 4:10 PM Subject: [R] Equivalents of Matlab's 'find' and 'end'> Sorry if these questions have been asked recently--I'm new to this > list. > > I'm primarily a Matlab user who is attempting to learn R and I'm > searching for possible equivalents of commands that I found very > handy in Matlab. So that I don't seem ungrateful to those who may > answer, I HAVE determined ways to carry out these processes in > 'brute force' sorts of ways in R code, but they lack the elegance > and simplicity of the Matlab commands. Also, if you know that no > such commands exist, that bit of knowledge would be helpful to know > so that I don't continue fruitless searches. > > The first is Matlab's 'find' command. > This is one of the most useful commands in Matab. Basically, if X > is the vector > > X=[3, 2, 1, 1, 2, 3] > > the command > > 'find(X==1)' > > would return the vector [3, 4] which would indicate that the vector > X had the value of 1 at the 3 and 4 positions. This was an > extremely useful command for subsetting in Matlab. The closest > thing I've found in R has been 'match' but match only returns the > first value as opposed to the position of all matching values. > > The second Matlab command that I'd like to find an R equivalent for > is 'end'. 'end' is just a simple little command that indicates the > end of a row/column. It is incredibly handy when used to subset > matrices like > > Y = X(2:end) > > and produces Y=[2, 1, 1, 2, 3] if the X is the same as in the > previous example. This cutsie little command was extremely useful > for composing programs that were flexible and could use input > matrices of any size without modifying the code. I realize that you > can accomplish the same by Y <- X[2:length(X)] in R, but this method > is ungainly, particularly when subsetting matrices rather than > vectors. > > If anyone has advice, I'd be grateful, > > Bryan L. Brown > Integrative Biology > University of Texas at Austin > Austin, TX 78712 > 512-965-0678 > stonefly at mail.utexas.edu > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
At Thursday 08:10 AM 10/7/2004, Bryan L. Brown wrote:>Sorry if these questions have been asked recently--I'm new to this list. > >I'm primarily a Matlab user who is attempting to learn R and I'm searching >for possible equivalents of commands that I found very handy in >Matlab. So that I don't seem ungrateful to those who may answer, I HAVE >determined ways to carry out these processes in 'brute force' sorts of >ways in R code, but they lack the elegance and simplicity of the Matlab >commands. Also, if you know that no such commands exist, that bit of >knowledge would be helpful to know so that I don't continue fruitless >searches. > >The first is Matlab's 'find' command. >This is one of the most useful commands in Matab. Basically, if X is the >vector > >X=[3, 2, 1, 1, 2, 3] > >the command > >'find(X==1)' > >would return the vector [3, 4] which would indicate that the vector X had >the value of 1 at the 3 and 4 positions. This was an extremely useful >command for subsetting in Matlab. The closest thing I've found in R has >been 'match' but match only returns the first value as opposed to the >position of all matching values.For this specific case, you can use which(). Also note that sometimes it can be useful to use match() with the arguments swapped, which can return you the positions of all matching values. Also, the operator %in% can be useful: > X <- c(3, 2, 1, 1, 2, 3) > which(X==1) [1] 3 4 > match(1, X) [1] 3 > match(X, 1) [1] NA NA 1 1 NA NA > which(!is.na(match(X, 1))) [1] 3 4 > which(X %in% 1) [1] 3 4 >>The second Matlab command that I'd like to find an R equivalent for is >'end'. 'end' is just a simple little command that indicates the end of a >row/column. It is incredibly handy when used to subset matrices like > >Y = X(2:end) > >and produces Y=[2, 1, 1, 2, 3] if the X is the same as in the previous >example. This cutsie little command was extremely useful for composing >programs that were flexible and could use input matrices of any size >without modifying the code. I realize that you can accomplish the same by >Y <- X[2:length(X)] in R, but this method is ungainly, particularly when >subsetting matrices rather than vectors.Yep, that is a handy feature, and I often wish for something like it, but in my 10 years of using R/S-PLUS I've not come across anything better than using length(X) (or nrow(X)/ncol(X)) for the general case. (But I do sometimes still discover useful things that I didn't know about.) For your specific case of > Y = X(2:end) in R/S-PLUS you can do: > Y = X[-1]>If anyone has advice, I'd be grateful, > >Bryan L. Brown >Integrative Biology >University of Texas at Austin >Austin, TX 78712 >512-965-0678 >stonefly at mail.utexas.edu > [[alternative HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Thursday 07 October 2004 09:10, Bryan L. Brown wrote:> Sorry if these questions have been asked recently--I'm new to this > list. > > I'm primarily a Matlab user who is attempting to learn R and I'm > searching for possible equivalents of commands that I found very > handy in Matlab. So that I don't seem ungrateful to those who may > answer, I HAVE determined ways to carry out these processes in 'brute > force' sorts of ways in R code, but they lack the elegance and > simplicity of the Matlab commands. Also, if you know that no such > commands exist, that bit of knowledge would be helpful to know so > that I don't continue fruitless searches. > > The first is Matlab's 'find' command. > This is one of the most useful commands in Matab. Basically, if X is > the vector > > X=[3, 2, 1, 1, 2, 3] > > the command > > 'find(X==1)'which(X==1)> would return the vector [3, 4] which would indicate that the vector X > had the value of 1 at the 3 and 4 positions. This was an extremely > useful command for subsetting in Matlab. The closest thing I've > found in R has been 'match' but match only returns the first value as > opposed to the position of all matching values. > > The second Matlab command that I'd like to find an R equivalent for > is 'end'. 'end' is just a simple little command that indicates the > end of a row/column. It is incredibly handy when used to subset > matrices like > > Y = X(2:end) > > and produces Y=[2, 1, 1, 2, 3] if the X is the same as in the > previous example. This cutsie little command was extremely useful > for composing programs that were flexible and could use input > matrices of any size without modifying the code. I realize that you > can accomplish the same by Y <- X[2:length(X)] in R, but this method > is ungainly, particularly when subsetting matrices rather than > vectors.I don't know of anything better than length(X) or nrow/ncol(X), but you could do X[-1], which is at least as elegant for your example, and would work for matrices as well. Hth, Deepayan
Bryan L. Brown wrote:> Sorry if these questions have been asked recently--I'm new to this > list.Hi Bryan, A useful resource is Robin Hankin's contributed document "R and Octave": http://cran.r-project.org/doc/contrib/R-and-octave-2.txt> The first is Matlab's 'find' command. This is one of the most useful > commands in Matab. Basically, if X is the vector > > X=[3, 2, 1, 1, 2, 3] > > the command > > 'find(X==1)' > > would return the vector [3, 4]> x <- c(3, 2, 1, 1, 2, 3) > which(x == 1) [1] 3 4> > The second Matlab command that I'd like to find an R equivalent for > is 'end'. 'end' is just a simple little command that indicates the > end of a row/column. It is incredibly handy when used to subset > matrices like > > Y = X(2:end) > > and produces Y=[2, 1, 1, 2, 3] if the X is the same as in the > previous example.> Y <- x[-1] > Y [1] 2 1 1 2 3 Not quite the same I'll grant you, but results in the same thing. If you wanted Y = X(4:end) you could use: > Y <- x[-c(1:3)] > Y [1] 1 2 3 HTH Gav -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [T] +44 (0)20 7679 5522 ENSIS Research Fellow [F] +44 (0)20 7679 7565 ENSIS Ltd. & ECRC [E] gavin.simpson at ucl.ac.uk UCL Department of Geography [W] http://www.ucl.ac.uk/~ucfagls/cv/ 26 Bedford Way [W] http://www.ucl.ac.uk/~ucfagls/ London. WC1H 0AP. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
On Thu, 7 Oct 2004, Bryan L. Brown wrote:> Sorry if these questions have been asked recently--I'm new to this list. > > I'm primarily a Matlab user who is attempting to learn R and I'm > searching for possible equivalents of commands that I found very handy > in Matlab. So that I don't seem ungrateful to those who may answer, I > HAVE determined ways to carry out these processes in 'brute force' sorts > of ways in R code, but they lack the elegance and simplicity of the > Matlab commands. Also, if you know that no such commands exist, that > bit of knowledge would be helpful to know so that I don't continue > fruitless searches. > > The first is Matlab's 'find' command.> This is one of the most useful commands in Matab. Basically, if X is > the vector > > X=[3, 2, 1, 1, 2, 3] > > the command > > 'find(X==1)' > > would return the vector [3, 4] which would indicate that the vector X > had the value of 1 at the 3 and 4 positions. This was an extremely > useful command for subsetting in Matlab. The closest thing I've found > in R has been 'match' but match only returns the first value as opposed > to the position of all matching values.which(X==1)> The second Matlab command that I'd like to find an R equivalent for is > 'end'. 'end' is just a simple little command that indicates the end of > a row/column. It is incredibly handy when used to subset matrices like > > Y = X(2:end) > > and produces Y=[2, 1, 1, 2, 3] if the X is the same as in the previous example. This cutsie little command was extremely useful for composing programs that were flexible and could use input matrices of any size without modifying the code. I realize that you can accomplish the same by Y <- X[2:length(X)] in R, but this method is ungainly, particularly when subsetting matrices rather than vectors.X[2:length(X)] but X[-1] would be easier in R.> If anyone has advice, I'd be grateful,Advice: read a good account of indexing in R: `An Introduction to R' is a good start. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
There's also the `cheat sheet' http://cran.r-project.org/doc/contrib/R-and-octave-2.txt that might be useful for Matlab speakers. Andy> From: Dimitris Rizopoulos > > Hi Bryan, > > 1. which(x==1) > 2. X[2:nrow(X),] or X[,2:ncol(X)] > > The "An Introduction to R" document is very usdeful for this kind of > things. > > I hope it helps. > > Best, > Dimitris > > ---- > Dimitris Rizopoulos > Ph.D. Student > Biostatistical Centre > School of Public Health > Catholic University of Leuven > > Address: Kapucijnenvoer 35, Leuven, Belgium > Tel: +32/16/396887 > Fax: +32/16/337015 > Web: http://www.med.kuleuven.ac.be/biostat/ > http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm > > > ----- Original Message ----- > From: "Bryan L. Brown" <stonefly at mail.utexas.edu> > To: <R-help at stat.math.ethz.ch> > Sent: Thursday, October 07, 2004 4:10 PM > Subject: [R] Equivalents of Matlab's 'find' and 'end' > > > > Sorry if these questions have been asked recently--I'm new to this > > list. > > > > I'm primarily a Matlab user who is attempting to learn R and I'm > > searching for possible equivalents of commands that I found very > > handy in Matlab. So that I don't seem ungrateful to those who may > > answer, I HAVE determined ways to carry out these processes in > > 'brute force' sorts of ways in R code, but they lack the elegance > > and simplicity of the Matlab commands. Also, if you know that no > > such commands exist, that bit of knowledge would be helpful to know > > so that I don't continue fruitless searches. > > > > The first is Matlab's 'find' command. > > This is one of the most useful commands in Matab. Basically, if X > > is the vector > > > > X=[3, 2, 1, 1, 2, 3] > > > > the command > > > > 'find(X==1)' > > > > would return the vector [3, 4] which would indicate that the vector > > X had the value of 1 at the 3 and 4 positions. This was an > > extremely useful command for subsetting in Matlab. The closest > > thing I've found in R has been 'match' but match only returns the > > first value as opposed to the position of all matching values. > > > > The second Matlab command that I'd like to find an R equivalent for > > is 'end'. 'end' is just a simple little command that indicates the > > end of a row/column. It is incredibly handy when used to subset > > matrices like > > > > Y = X(2:end) > > > > and produces Y=[2, 1, 1, 2, 3] if the X is the same as in the > > previous example. This cutsie little command was extremely useful > > for composing programs that were flexible and could use input > > matrices of any size without modifying the code. I realize > that you > > can accomplish the same by Y <- X[2:length(X)] in R, but > this method > > is ungainly, particularly when subsetting matrices rather than > > vectors. > > > > If anyone has advice, I'd be grateful, > > > > Bryan L. Brown > > Integrative Biology > > University of Texas at Austin > > Austin, TX 78712 > > 512-965-0678 > > stonefly at mail.utexas.edu > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Bryan L. Brown <stonefly <at> mail.utexas.edu> writes: : : Sorry if these questions have been asked recently--I'm new to this list. : : I'm primarily a Matlab user who is attempting to learn R and I'm searching for possible equivalents of : commands that I found very handy in Matlab. So that I don't seem ungrateful to those who may answer, I HAVE : determined ways to carry out these processes in 'brute force' sorts of ways in R code, but they lack the : elegance and simplicity of the Matlab commands. Also, if you know that no such commands exist, that bit of : knowledge would be helpful to know so that I don't continue fruitless searches. : : The first is Matlab's 'find' command. : This is one of the most useful commands in Matab. Basically, if X is the vector : : X=[3, 2, 1, 1, 2, 3] : : the command : : 'find(X==1)' : : would return the vector [3, 4] which would indicate that the vector X had the value of 1 at the 3 and 4 : positions. This was an extremely useful command for subsetting in Matlab. The closest thing I've found in : R has been 'match' but match only returns the first value as opposed to the position of all matching values. : : The second Matlab command that I'd like to find an R equivalent for is 'end'. 'end' is just a simple little : command that indicates the end of a row/column. It is incredibly handy when used to subset matrices like : : Y = X(2:end) : : and produces Y=[2, 1, 1, 2, 3] if the X is the same as in the previous example. This cutsie little command was : extremely useful for composing programs that were flexible and could use input matrices of any size : without modifying the code. I realize that you can accomplish the same by Y <- X[2:length(X)] in R, but this : method is ungainly, particularly when subsetting matrices rather than vectors. : : If anyone has advice, I'd be grateful, In addition to the answers you already got there are: X[-1] # all but first element of a vector mat[,-1] # all but first column mat[-1,] # all but first row tail(X, 8) # last 8 elements of vector or last 8 rows of data frame Also be sure to check out Robin Hankin's "R and Octave" referenced at: http://cran.r-project.org/other-docs.html
Bryan you may find R-and-octave.txt useful here. This is a list of Matlab/Octave commands, and their R equivalents. It is in the "contributed docs" section of CRAN. HTH rksh>Sorry if these questions have been asked recently--I'm new to this list. > >I'm primarily a Matlab user who is attempting to learn R and I'm >searching for possible equivalents of commands that I found very >handy in Matlab. So that I don't seem ungrateful to those who may >answer, I HAVE determined ways to carry out these processes in >'brute force' sorts of ways in R code, but they lack the elegance >and simplicity of the Matlab commands. Also, if you know that no >such commands exist, that bit of knowledge would be helpful to know >so that I don't continue fruitless searches. > >The f-- Robin Hankin Uncertainty Analyst Southampton Oceanography Centre SO14 3ZH tel +44(0)23-8059-7743 initialDOTsurname at soc.soton.ac.uk (edit in obvious way; spam precaution)