1 year ago

#76718

test-img

Bill Pearson

R - inserting aggregate results into a data.frame

To produce a cumulative plot using ggplot2 geom_stat(), I need a data.frame() that has the number of instances of various combinations of factors. I know how to produce the numbers using aggregate(), e.g.

print(aggregate(cbind(count=prop_cost) ~ tax_cnt + data_set, data=out_data, FUN=function(x){NROW(x)}))

Gives me:

  tax_cnt data_set count
1       3    5taxa  1936
2       4    5taxa  3907
3       5    5taxa  7205
4       3  5taxaRS  1446
5       4  5taxaRS  2896
6       5  5taxaRS  6168

But how can I put these values back into the data.frame I am using to plot things? I would like to set a new column, $nt_cnt, so that

df[df$data_set=='5taxa' & df$tax_cnt==1,]$nt_cnt = 1936

and similarly for the other 5 sums.

This seems like it must be easy, but I need help.

r

dataframe

join

merge

0 Answers

Your Answer

Accepted video resources