Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a column of means by group to original data [duplicate]

Tags:

I want to add a column of means based on factor column in R data.frame. Like this:

df1 <- data.frame(X = rep(x = LETTERS[1:2], each = 3), Y = 1:6)
df2 <- aggregate(data = df1, Y ~ X, FUN = mean)
df3 <- merge(x = df1, y = df2, by = "X", suffixes = c(".Old",".New"))
df3
#   X Y.Old Y.New
# 1 A     1     2
# 2 A     2     2
# 3 A     3     2
# 4 B     4     5
# 5 B     5     5
# 6 B     6     5

To accomplish this problem I've to create two unnecessary data.frames. I'd like to know a way to append a column of means by factor column into my original data.frame without creating any extra data.frames. Thanks for your time and help.

like image 428
MYaseen208 Avatar asked Nov 02 '11 05:11

MYaseen208


People also ask

How do I add a column to a Dataframe in R?

1 Adding new columns. You can add new columns to a dataframe using the $ and assignment <- operators. To do this, just use the df$name notation and assign a new vector of data to it. As you can see, survey has a new column with the name sex with the values we specified earlier.


2 Answers

Two alternative ways of doing this:

1) with the dplyr package:

library(dplyr)
df1 <- df1 %>% 
  group_by(X) %>% 
  mutate(Y.new = mean(Y))

2) with the data.table package:

library(data.table)
setDT(df1)[, Y.new := mean(Y), by = X]

both give the following result:

> df1
   X Y Y.new
1: A 1     2
2: A 2     2
3: A 3     2
4: B 4     5
5: B 5     5
6: B 6     5
like image 82
Jaap Avatar answered Oct 18 '22 21:10

Jaap


This is what the ave function is for.

df1$Y.New <- ave(df1$Y, df1$X)
like image 24
John Avatar answered Oct 18 '22 22:10

John