Displaying 2 results from an estimated 2 matches for "mbn2".
Did you mean:
mb2
2011 Oct 05
2
aggregate function with a dataframe for both "x" and "by"
...at is a more elegant or "better" way to
accomplish this task?
Thanks!
mydata = data.frame(testvar1=c(1,3,5,7,8,3,5,NA,4,5,7,9),
testvar2=c(11,33,55,77,88,33,55,NA,44,55,77,99)
)
mybys=data.frame(mbn1=c('red','blue',1,2,NA,'big',1,2,'red',1,NA,12),mbn2=c('wet','dry',99,95,NA,'damp',95,99,'red',99,NA,NA)
, stringsAsFactors =F)
myaggs <- data.frame(matrix(data=NA, nrow=nrow(mydata), ncol=ncol(mydata) )
)
for(i in 1: ncol(mydata) ) {
temp <- aggregate(mydata[i], by = as.list(mybys[i]), FUN=sum, na.rm=T)...
2007 Jul 31
1
aggregate.data.frame - prevent conversion to factors? show statistics for NA values of "by" variable?
...,'red',1,NA,12)
myby2=c('wet','dry',99,95,NA,'damp',95,99,'red',99,NA,NA)
myby1.new = ifelse(is.na(myby1)==T,"is_na",myby1)
myby2.new = ifelse(is.na(myby2)==T,"is_na",myby2)
str(myby1.new)
str(myby2.new)
mybys=data.frame(mbn1=myby1.new,mbn2=myby2.new , stringsAsFactors =F)
str(mybys)
#
myagg1 = aggregate(x=mydf, by=mybys, FUN='mean')
str(myagg1)
myagg2 = myagg1
myagg2[1:ncol(mybys)] = as.character(unlist(myagg1[1:ncol(mybys)]))
str(myagg2)
myagg1
myagg2
---------------------------------
[[alternative HTML versi...