Thanks user1269942. I did like you said.
I'm answering my own question only to post the R code that I made to solve the problem. It's not fast and it could be done better, but it works.
First of all, I've increased the resolution of the target raster 20 times (in both directions) and then I've rasterized the vector layer using that resolution. "vegetazione.asc" is the exported raster map in AAIGrid format with 6 row header. "scala" is the ration between rasterized map and original resolution (in this case 20). The file "progress.txt" that I write is made only to check the progress of the code.
vegetazione <- read.table("Scrivania/Vegetazione.asc",skip=6,na.strings = "255")
scala <- 20
target <- data.frame(matrix(ncol = length(vegetazione[1,])/scala,nrow = length(vegetazione[,1])/scala))
for(i in 1:(length(vegetazione[,1])/scala)){
for(j in 1:(length(vegetazione[1,])/scala)){
cont <- 0
for(k in ((i-1)*scala+1):(i*scala)){
for(l in ((j-1)*scala+1):(j*scala)){
if(!is.na(vegetazione[k,l])){
cont <- cont+1
}
}
}
target[i,j] <- cont
write.table(j,"Scrivania/progress.txt",append=TRUE,quote = FALSE,row.names = FALSE,col.names = FALSE)
}
}
target <- target/(scala*scala)
"target" is the output to be saved.
PS: in my case the "vegetazione" map has 16540*3360 cells and it takes about 3 hours to run. Maybe it can be parallelized to improve the performance