I have a matrix with a set of variables one of which is a factor. Using by()
I have calculated something about each group (say the sum). Now I want to
create a new variable in the original matrix that contains the results of
the by() for each observation that is in the corresponding group.
For example I have:
---------
a <-c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
b <-c(7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)
e <-c("A","B","C","D","E","F","A","B","C","D","E","F","D","E","F","A")
f <-data.frame(e,a,b)
# Calculate sum by group
sums <- by(f, e, function(x) sum(x$b))
-----------
Now I would like to assign each observation in f a new variable based on the
results of the by(). I converted sums into a matrix and then tried using
match() and ifthen() but could not get it to work.
Thanks,
EG
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.