I ended up with a big data.table and I want to apply a function per row. It works with data.frame and apply, but I want to get a solution with data.table.
>sdata
Z_2016_11 Z_2016_10 Z_2016_09 Z_2016_08 Z_2016_07
1: 21 32 15 NA NA
2: 6 17 NA NA NA
3: 2 7 9 230 NA
4: 5 19 28 30 0
5: 16 29 30 105 0
6: 2 0 0 0 NA
I want to order it as following
>sdata
Z_1 Z_2 Z_3 Z_4 Z_5
1: 15 32 21 NA NA
2: 17 6 NA NA NA
3: 230 9 7 2 NA
4: 0 30 28 19 5
5: 0 105 30 29 16
6: 0 0 0 2 NA
With a data.frame I can use
t(apply(sdata[, grep("Z", names(sdata), value = TRUE),with=FALSE],1,
function(tmp) c(rev(tmp[!is.na(tmp)]),
rep(NA, times = length(tmp) - length(tmp[!is.na(tmp)])))))
I tried it the following way
trimt <- function(tmp){
c(rev(tmp[!is.na(tmp)]), rep(NA, times = length(tmp) - length(tmp[!is.na(tmp)])))
}
sdata[,trimt(get(grep("Z", names(sdata), value = TRUE))),by=1:nrow(sdata)]
So I can order it easily, but only a vector returns. Is it possible to use a data.table similar to apply and return a new matrix?