[R] Basic Vector and Matrix Operations

Xiaohui Chen chenxh007 at gmail.com
Mon Jul 7 23:17:30 CEST 2008


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.
>
>



More information about the R-help mailing list