r - How to store index values using dplyr function -
the data (dat) have collected has 2 different buckets part of same set ("set") filled random number of marbles ("marbles") there 2 treatments, "color" (blue/white) , "size" (small/large). each trial done in different "blocks" (ie. time slot). 2 buckets part of 1 set. position of buckets noted ("position"). here sample of data.
colour set size position marbles block blue 1 small 1 8 1 blue 1 small 2 81 1 blue 6 small 1 14 2 blue 6 small 2 11 2 blue 1 large 4 0 1 blue 1 large 5 0 1 blue 1 large 1 3 1 blue 1 large 2 43 1 white 1 small 1 8 1 white 1 small 2 81 1 white 1 small 6 7 1 white 6 small 3 12 2 white 6 small 4 25 2 white 2 large 1 86 1 white 2 large 2 77 1 white 1 large 1 3 11 white 1 large 2 43 11
for each unique replicate (i.e. unique combination of color, set, block, size), want find index position of max value of marbles.
library(dplyr) library(plyr) <- vector() result <- dat %>% mutate(maxmarbles = max(dat$marbles)) %>% group_by(colour, size, block, set) %>% pos1 <- which(a == max(dat$marbles))
i keep running error says not find "%>%". verified installation of dplyr , ensured called library. however, unable detect problem , wondering perhaps syntax error i'm unable catch?
first rule: never ever load plyr after dplyr :)
now question. indices of rows have maximum number of marbles within group:
library(dplyr) result_df <- dat %>% group_by(colour, size, block, set) %>% mutate(result = (marbles == max(marbles))) result_vec <- which(result_df$result)
if want boolean indices can omit which()
.
Comments
Post a Comment