Hello, a data.frame, df, holds the numerics, x, y, and z. A function, fun, should return some arbitrary statistics about the arguments, e.g. the sum or anything else. What I want to do is to apply this function to every pair of variables in df, and the return should be a matrix as found with cov. How can I achieve that? Thanks, S?ren df <- data.frame(x=1:10, y=11:20, z=21:30); fun <- function(x){ return(sum(x)); } # and now???
Hi Soeren Apply or aggregate functions best regards M Soeren.Vogel at eawag.ch a ?crit :> Hello, a data.frame, df, holds the numerics, x, y, and z. A function, > fun, should return some arbitrary statistics about the arguments, e.g. > the sum or anything else. What I want to do is to apply this function > to every pair of variables in df, and the return should be a matrix as > found with cov. How can I achieve that? Thanks, S?ren > > df <- data.frame(x=1:10, y=11:20, z=21:30); > fun <- function(x){ > return(sum(x)); > } > # and now??? > > ______________________________________________ > 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. >-- Mohamed Lajnef,IE INSERM U955 eq 15 P?le de Psychiatrie H?pital CHENEVIER 40, rue Mesly 94010 CRETEIL Cedex FRANCE Mohamed.lajnef at inserm.fr tel : 01 49 81 31 31 (poste 18470) Sec : 01 49 81 32 90 fax : 01 49 81 30 99
Hi Mohamed, thanks for your answer. Anyway, the "how to" is exactly my problem, since ... fun2 <- function(x){ please_use_aggregate_and_apply_in_some_way_and_return_the_output_of_my_example_as_requested (fun(x)); } fun2(df); ... unfortunately returns an error ;-). Could you please give a simple example? Thanks, S?ren On 30.04.2010, at 12:59, Mohamed Lajnef wrote:> Hi Soeren > > Apply or aggregate functions > > best regards > M > Soeren.Vogel at eawag.ch a ?crit : >> Hello, a data.frame, df, holds the numerics, x, y, and z. A >> function, fun, should return some arbitrary statistics about the >> arguments, e.g. the sum or anything else. What I want to do is to >> apply this function to every pair of variables in df, and the >> return should be a matrix as found with cov. How can I achieve >> that? Thanks, S?ren >> >> df <- data.frame(x=1:10, y=11:20, z=21:30); >> fun <- function(x){ >> return(sum(x)); >> } >> # and now???
On 30/04/2010 7:55 AM, Soeren.Vogel at eawag.ch wrote:> Hi Mohamed, thanks for your answer. Anyway, the "how to" is exactly my > problem, since ... > > fun2 <- function(x){ > > please_use_aggregate_and_apply_in_some_way_and_return_the_output_of_my_example_as_requested > (fun(x)); > } > fun2(df); > > ... unfortunately returns an error ;-). Could you please give a simple > example? Thanks, S?ren >I would have recommended a nested for loop, but here it is with apply: > df <- data.frame(a=1:10, b=11:20, c=21:30) > f <- function(x,y) sum(x*y) > apply(df, 2, function(x) apply(df, 2, function(y) f(x,y))) a b c a 385 935 1485 b 935 2485 4035 c 1485 4035 6585 To me this looks clearer: result <- matrix(NA, 3,3) for (i in 1:3) for (j in 1:3) result[i,j] <- f(df[,i], df[,j]) Duncan Murdoch> On 30.04.2010, at 12:59, Mohamed Lajnef wrote: > > >> Hi Soeren >> >> Apply or aggregate functions >> >> best regards >> M >> Soeren.Vogel at eawag.ch a ?crit : >> >>> Hello, a data.frame, df, holds the numerics, x, y, and z. A >>> function, fun, should return some arbitrary statistics about the >>> arguments, e.g. the sum or anything else. What I want to do is to >>> apply this function to every pair of variables in df, and the >>> return should be a matrix as found with cov. How can I achieve >>> that? Thanks, S?ren >>> >>> df <- data.frame(x=1:10, y=11:20, z=21:30); >>> fun <- function(x){ >>> return(sum(x)); >>> } >>> # and now??? >>> > > ______________________________________________ > 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. >
On Apr 30, 2010, at 6:59 AM, Mohamed Lajnef wrote:> Hi Soeren > > Apply or aggregate functions >Probably needs combn as well. Could do it all with numeric indices, but this effort with character vectors seems acceptable: fun <- function(x){ cnms <- colnames(x) return(apply(combn(cnms,2), 2, function(y) sum(x[,y]))) } fun(df) #[1] 210 310 410 I do have a question about "returning a matrix" though. Did you mena that you wanted the pairs of sums rather than the sums of pairs. In that case: fun2 <- function(x){cnms <- colnames(x) return(apply(combn(cnms,2), c(1,2), function(y) sum(x[,y]))) } fun2(df) # [,1] [,2] [,3] #[1,] 55 55 155 #[2,] 155 255 255 -- David.> best regards > M > Soeren.Vogel at eawag.ch a ?crit : >> Hello, a data.frame, df, holds the numerics, x, y, and z. A >> function, fun, should return some arbitrary statistics about the >> arguments, e.g. the sum or anything else. What I want to do is to >> apply this function to every pair of variables in df, and the >> return should be a matrix as found with cov. How can I achieve >> that? Thanks, S?ren >> >> df <- data.frame(x=1:10, y=11:20, z=21:30); >> fun <- function(x){ >> return(sum(x)); >> } >> # and now??? >> >> ______________________________________________ >> 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. >> > > > -- > > > Mohamed Lajnef,IE INSERM U955 eq 15 > P?le de Psychiatrie > H?pital CHENEVIER > 40, rue Mesly > 94010 CRETEIL Cedex FRANCE > Mohamed.lajnef at inserm.fr > tel : 01 49 81 31 31 (poste 18470) > Sec : 01 49 81 32 90 > fax : 01 49 81 30 99 > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Thanks for the code, that was exactly what I was looking for. Regards, S?ren On 30.04.2010, at 14:04, David Winsemius wrote:> > On Apr 30, 2010, at 6:59 AM, Mohamed Lajnef wrote: > >> Hi Soeren >> >> Apply or aggregate functions >> > > Probably needs combn as well. Could do it all with numeric indices, > but this effort with character vectors seems acceptable: > > fun <- function(x){ cnms <- colnames(x) > return(apply(combn(cnms,2), 2, function(y) sum(x[,y]))) > } > fun(df) > #[1] 210 310 410 > > I do have a question about "returning a matrix" though. Did you mena > that you wanted the pairs of sums rather than the sums of pairs. In > that case: > > fun2 <- function(x){cnms <- colnames(x) > return(apply(combn(cnms,2), c(1,2), function(y) sum(x[,y]))) > } > > fun2(df) > # [,1] [,2] [,3] > #[1,] 55 55 155 > #[2,] 155 255 255 > > -- > David. > >> best regards >> M >> Soeren.Vogel at eawag.ch a ?crit : >>> Hello, a data.frame, df, holds the numerics, x, y, and z. A >>> function, fun, should return some arbitrary statistics about the >>> arguments, e.g. the sum or anything else. What I want to do is to >>> apply this function to every pair of variables in df, and the >>> return should be a matrix as found with cov. How can I achieve >>> that? Thanks, S?ren >>> >>> df <- data.frame(x=1:10, y=11:20, z=21:30); >>> fun <- function(x){ >>> return(sum(x)); >>> } >>> # and now??? >>> >> >> -- >> >> >> Mohamed Lajnef,IE INSERM U955 eq 15 >> P?le de Psychiatrie >> H?pital CHENEVIER >> 40, rue Mesly >> 94010 CRETEIL Cedex FRANCE >> Mohamed.lajnef at inserm.fr >> tel : 01 49 81 31 31 (poste 18470) >> Sec : 01 49 81 32 90 >> fax : 01 49 81 30 99 > > David Winsemius, MD > West Hartford, CT > > >-- S?ren Vogel, Dipl.-Psych. (Univ.), PhD-Student, Eawag, Dept. SIAM http://www.eawag.ch, http://sozmod.eawag.ch