I have a large matrix with over 1000 rows and 100 columns. Each row contains ONLY 6-10 columns that have values, and the rest are zeros. I want to create a matrix that has only 5 columns that take values โโof 5 consecutive columns in each row. For instance:
A = structure(c(0, 1L, 6L, 0, 2L, 0, 2L, 0, 1L, 4L, 1L, 3L, 7L, 2L, 6L, 2L, 4L, 0, 3L, 0, 3L, 5L, 1L, 4L, 0, 4L, 6L, 2L, 0, 0, 5L, 0, 3L, 5L, 0, 0, 0, 4L, 6L, 7L, 0, 7L, 5L, 7L, 8L, 6L, 0, 0, 8L, 9L, 0, 0, 0, 9L, 1L, 0 , 0, 0, 0, 2L, 7L, 0, 2L, 0, 0, 1L, 8L, 4, 0, 0), .Dim = c(5L, 14L))
I want this matrix:
B = structure(c(1L, 1L, 1L, 5L, 7L, 2L, 2L, 2L, 6L, 8L, 3L, 3L, 3L, 7L, 9L, 4L, 4L, 4L, 8L, 1L, 5L, 5L, 5L, 9L, 2L), .Dim = c(5L, 5L))
My code is:
df = data.frame(A) B = do.call(rbind, lapply(1:NROW(df), function(i) df[i,][(df[i,])!=0][1:5])) # or B = t(apply(X = df, MARGIN = 1, function(x) x[x!=0][1:5]))
My code works fine for the first two lines of A, but it doesn't work for the rest of the lines. I also thought about getting indexes on columns that do not have zeros, and then to see if there are 5 consecutive columns (without any gap between them) and get their values. Any help is much appreciated!