dimanche 16 septembre 2018

R - matrix : function "if " applied to each cell

I'm trying perform a function to each cell of a data table in R. I want to add the cells per 3 if they are >0. Exemple :

mat <- matrix(data=0:9, nrow=5, ncol=10, byrow=F)
mat3 <- apply(mat, MARGIN = 1, FUN= function(mat) if(mat != 0) {mat+3})

But first that created a list of lenght 5 and second it's all the cells who are added per 3.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire