[R] How do I make proper use of the by() function?

Anupam anupamtg at gmail.com
Sat Jun 11 07:00:45 CEST 2011


You may want to read about several "apply" functions. They may help you do
what you are trying.

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Fredrik Karlsson
Sent: Saturday, June 11, 2011 3:13 AM
To: R-Help List
Subject: [R] How do I make proper use of the by() function?

Dear list,

I have a function that uses values from two vectors  and spits out one new
vector  based on all the values of the two original vectors, and with the
same length as them.
Ok, I would now like to be able to apply that function simply on two columns
in a data frame, divided by the levels of factors in the data frame.

I'm trying to use by() for this, but the output is too hard to use. What I
am doing is this:

by(df, list(df$Factor1,df$Factor2),function(x)
my_function(x$col1,x$col2),simplify=TRUE)

and the output is too complex to be used in a simple way. Actually, I just
want something like a data frame, where the results vectors are placed in
one column and the conditions under which they were produced (i.e. the
values of the factors according to which the data set were divided) in other
columns.

This does not seem to be provided by by(), and aggregate only provides me
with the ability to use values from one column, right?
So, are there other functions I could use?

Thanks!

/Fredrik

--
"Life is like a trumpet - if you don't put anything into it, you don't get
anything out of it."

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org 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.



More information about the R-help mailing list