I got the matrix time down by another factor of 4
tmp <- matrix(c(0,0,1,0,0,
NA,0,1,1,0,
0,1,0,0,NA,
1,0,1,0,1), ## last item in row has value 1
byrow=TRUE, 4, 5)
## Jeff matrix
DF2 <- DF <- tmp
DF2[ , -1 ] <- ifelse( !is.na( DF[ ,
Another approach using in-place replacement and thinking with matrix
operations instead of vector operations:
DF <- matrix( c( 0, 0, 1, 0, 0
, NA, 0, 1, 1, 0
, 0, 1, 0, 0, NA )
, byrow=TRUE
, nrow=3 )
DF2 <- DF
DF2[ , -1 ] <- ifelse( !is.
which(df == 1, arr.ind=TRUE) is useful here:
> df <- matrix(c(0,NA,0,0,0,1,1,1,0,0,1,0,0,0,NA), nrow=3)
> df
[,1] [,2] [,3] [,4] [,5]
[1,]00100
[2,] NA0110
[3,]0100 NA
> ## Identify (row,col) indices for 1:s
> idxs <- which(df == 1, arr
Hi Saba,
Try this:
df<-matrix(c(0,NA,0,0,0,1,1,1,0,0,1,0,0,0,NA),nrow=3)
dimdf<-dim(df)
df1<-df==1
df[cbind(rep(FALSE,dimdf[1]),df1[,-dimdf[2]])]<-1
Jim
On Fri, Sep 23, 2016 at 12:27 PM, Saba Sehrish via R-help
wrote:
> Hi
>
> I have a matrix that contains 1565 rows and 132 columns. All the o
tmpf <- function(x) {
n <- length(x)
indices <- which(match(x,1) == 1)
x[indices+1] <- 1
x[1:n] ## needed for the case when the last item in a row has value 1
}
tmp <- matrix(c(0,0,1,0,0,
NA,0,1,1,0,
0,1,0,0,NA,
1,0,1,0,1), ## last item in r
5 matches
Mail list logo