Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a column containing aggregated means with R? [duplicate]

In R, I have a bunch of data in a dataframe like:

state | zip   | value
______|_______|______
CA    | 94555 | 18
CA    | 94556 | 5
OH    | 12345 | 22
OH    | 12346 | 10

and so on.

I want an add a column to each row listing the mean 'value' for that state.

I can get a dataframe of the means via "(aggregate(data$value, list(State = data$state), mean))". That gives me a dataframe with 50 rows, one for each state. But I need to then go back into the original dataframe and put the state's average in rows belonging to that state.

How would I go about doing this?

like image 485
int3h Avatar asked Nov 28 '25 06:11

int3h


1 Answers

And a data.table solution

library(data.table)
DT <-  data.table(state = c("CA","CA","OH","OH"), 
                   zip = c(94555,94556,12345,12346), 
                   value = c(18, 5, 22, 10))

DT[, mean := mean(value), by = state]

##    state   zip value mean
## 1:    CA 94555    18 11.5
## 2:    CA 94556     5 11.5
## 3:    OH 12345    22 16.0
## 4:    OH 12346    10 16.0
like image 138
mnel Avatar answered Nov 30 '25 20:11

mnel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!