R - How to select dominant b1-value and count(b2) and make b2 values column headings -
sample data:
a <- as.data.frame(matrix(list(1,1,3,4,1,1,3,4,1,1,3,4,1,1,3,4,1,1,3,4),4,5)) b1 <-c(30,40,20,15) b2<-c("a","a","b","c") b <-as.data.frame(cbind(b1,b2)) a.b<-cbind(a,b)
inital value: a.b
row v1 v2 v3 v4 v5 b1 b2 1 1 1 1 1 1 30 2 1 1 1 1 1 40 3 3 3 3 3 3 20 b 4 4 4 4 4 4 15 c
what see v1, v2, v3, v4, v5, b1 max(b1), a, b, c there should 3 rows:
row v1 v2 v3 v4 v5 b1 b c 1 1 1 1 1 1 40 2 0 0 3 3 3 3 3 3 20 0 1 0 4 4 4 4 4 4 15 0 0 1
how go getting that. have thought of aggregate, cast, , reshape, have run difficulty. thks.
this looks lapply(split(...)) problem. untested , assumes not use as.data.frame(cbind(...))
lapply(split(df, df[2:6]), function (d){ cbind( max( d[7]), table(d[8]) )})
the reason not use character comparison "4" > "15" returns true.
Comments
Post a Comment