I am wondering if it is possible to perform the following two basic functions with primitive R functions. I know I could write functions for either, but it seems as though they are probably built-in somewhere. 1) Fill out a vector to a desired length with missing values or zeros. So, x<-c(3,4,5) f(x,5) 3,4,5,NA,NA 2) Find the [row,col] location of a particular value in a matrix, eg x<-matrix(1:9,ncol=3) f(x,2) 2,1 Thanks. -- Eric Turkheimer, PhD Department of Psychology University of Virginia PO Box 400400 Charlottesville, VA 22904-4400 http://www.people.virginia.edu/~ent3c 434-982-4732 434-982-4766 (FAX) [[alternative HTML version deleted]]
Eric Turkheimer ??:> I am wondering if it is possible to perform the following two basic > functions with primitive R functions. I know I could write functions for > either, but it seems as though they are probably built-in somewhere. > > 1) Fill out a vector to a desired length with missing values or zeros. So, > > x<-c(3,4,5) > f(x,5) > > 3,4,5,NA,NA >Dunno if any buildin function in R, but c(x,rep(NA,5-length(x))) seems to be simple> 2) Find the [row,col] location of a particular value in a matrix, eg > > x<-matrix(1:9,ncol=3) > > f(x,2) > > 2,1 >which(2==x,arr.ind=T)> > Thanks. > >
Here is a solution: 1. c(x, rep(NA, n-length(x))) 2. which(x==2, arr.ind=TRUE) Cheers Andy __________________________________ Andy Jaworski 518-1-01 Process Laboratory 3M Corporate Research Laboratory ----- E-mail: apjaworski@mmm.com Tel: (651) 733-6092 Fax: (651) 736-3122 "Eric Turkheimer" <ent3c@virginia.edu> Sent by: r-help-bounces@r-project.org 07/07/2008 04:09 PM To r-help@r-project.org cc Subject [R] Basic Vector and Matrix Operations I am wondering if it is possible to perform the following two basic functions with primitive R functions. I know I could write functions for either, but it seems as though they are probably built-in somewhere. 1) Fill out a vector to a desired length with missing values or zeros. So, x<-c(3,4,5) f(x,5) 3,4,5,NA,NA 2) Find the [row,col] location of a particular value in a matrix, eg x<-matrix(1:9,ncol=3) f(x,2) 2,1 Thanks. -- Eric Turkheimer, PhD Department of Psychology University of Virginia PO Box 400400 Charlottesville, VA 22904-4400 http://www.people.virginia.edu/~ent3c 434-982-4732 434-982-4766 (FAX) [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list 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]]
Dear Eric, Try this: # First function f1=function(x,k) c(x,rep(NA,k-length(x))) x<-c(3,4,5) f1(x,5) # Second function f2=function(x,val){ res=which(x==val,arr.ind=T) paste(res,collapse=',',sep='') } x<-matrix(1:9,ncol=3) f2(x,2) HTH, Jorge On Mon, Jul 7, 2008 at 5:07 PM, Eric Turkheimer <ent3c@virginia.edu> wrote:> I am wondering if it is possible to perform the following two basic > functions with primitive R functions. I know I could write functions for > either, but it seems as though they are probably built-in somewhere. > > 1) Fill out a vector to a desired length with missing values or zeros. > So, > > x<-c(3,4,5) > f(x,5) > > 3,4,5,NA,NA > > 2) Find the [row,col] location of a particular value in a matrix, eg > > x<-matrix(1:9,ncol=3) > > f(x,2) > > 2,1 > > > Thanks. > > -- > Eric Turkheimer, PhD > Department of Psychology > University of Virginia > PO Box 400400 > Charlottesville, VA 22904-4400 > > http://www.people.virginia.edu/~ent3c<http://www.people.virginia.edu/%7Eent3c> > > 434-982-4732 > 434-982-4766 (FAX) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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]]
Eric Turkheimer wrote:> I am wondering if it is possible to perform the following two basic > functions with primitive R functions. I know I could write functions for > either, but it seems as though they are probably built-in somewhere. > > 1) Fill out a vector to a desired length with missing values or zeros. So, > > x<-c(3,4,5) > f(x,5) > > 3,4,5,NA,NA >For the NA case, see ?length.> 2) Find the [row,col] location of a particular value in a matrix, eg > > x<-matrix(1:9,ncol=3) > > f(x,2) > > 2,1 > > > Thanks. >
For the first case, just extend the vector:> x<-c(3,4,5) > x[1] 3 4 5> x[5] <- NA > x[1] 3 4 5 NA NA>the second would use 'which' On Mon, Jul 7, 2008 at 5:07 PM, Eric Turkheimer <ent3c at virginia.edu> wrote:> I am wondering if it is possible to perform the following two basic > functions with primitive R functions. I know I could write functions for > either, but it seems as though they are probably built-in somewhere. > > 1) Fill out a vector to a desired length with missing values or zeros. So, > > x<-c(3,4,5) > f(x,5) > > 3,4,5,NA,NA > > 2) Find the [row,col] location of a particular value in a matrix, eg > > x<-matrix(1:9,ncol=3) > > f(x,2) > > 2,1 > > > Thanks. > > -- > Eric Turkheimer, PhD > Department of Psychology > University of Virginia > PO Box 400400 > Charlottesville, VA 22904-4400 > > http://www.people.virginia.edu/~ent3c > > 434-982-4732 > 434-982-4766 (FAX) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?