Could somebody tell me how to create an aggregate function for dcast that would return the first non-NA value?
Something along these lines (while the example is obviously not working)
wide<-dcast(long, usr_id ~ variable, value.var = "value", fun.aggregate = head, 1, na.rm=true)
The data could look like this:
set.seed(28)
long <- data.frame(
usr_id= rep(1:2, each=4),
variable= rep(c('M1','M2'), 4),
value= sample(c(NA,1:2),2*4, replace=TRUE)
)
We can try
dcast(long, usr_id~variable, value.var="value",
fun.aggregate= function(x) head(x[!is.na(x)], 1), fill=0)
set.seed(28)
long <- data.frame(usr_id= rep(1:3, each=3),
variable= rep(paste0("M", 1:3), 3), value= sample(c(NA,
1:3),9, replace=TRUE))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.