r - Melt able to remove NA's for some variables but not others? -
i have question regarding melt function reshape2 package in r.
i melt multi-dimensional array, keeping na's of variables, removing na's rest. example illustrate don't want:
library(reshape2) subtotals <- tapply( mtcars$mpg, list(mtcars$cyl,mtcars$disp,mtcars$hp,mtcars$drat,mtcars$wt), sum ) subtotals_melted <- melt(subtotals, na.rm = t) # removes na's vars
i know there na.rm=
option in melt
function, removes na
's variables. such subtotal-ing operations typically take quite bit of memory due implicit na
's, after being melted, wondering if there faster , more memory-efficient way implement such use-case.
thanks in advance!
library(dplyr) library(magrittr) result = mtcars %>% group_by(cyl, disp, hp, drat, wt) %>% summarize(sum_mpg = sum(mpg, na.rm = true) )
this preserve na levels in grouping variables not mpg. in general, though, agree michael bellhouse conditional mutation best done separating 2 separate dataframes.
Comments
Post a Comment