Re: [R] error using lapply with oneway_test (coin package)
On 07/05/2009 6:11 AM, Matthieu Dubois wrote: Dear expeRts, I would like to use a oneway_test (from package coin) to test whether two groups differ on various variables. The variables are encoded within a data frame. Unfortunately, I obtained an error, that I don't understand. Could you please help me ? Example: library(coin) y - as.data.frame(matrix(rnorm(200), ncol=2)) group - as.factor(unif(100)) lapply(y, function(var) oneway_test(var ~ group)) Error message is: (in French, my locale) Erreur dans eval(expr, envir, enclos) : objet 'var' introuvable (my personal translation in English) Error in eval(expr, envir, enclos) : object 'var' not found That's a scoping problem, I think a bug in oneway_test. Because the formula var ~ group is created with the anonymous function within lapply, its environment should be the evaluation frame of that function call and var should be visible. If I replace oneway_test() with lm() it works. I think a workaround is to construct the data argument explicitly, i.e. lapply(y, function(var) oneway_test(var ~ group, data.frame(var=var, group=group))) I've cc'd Torsten Hothorn, the maintainer of coin. Duncan Murdoch Thank you, Matthieu Matthieu Dubois Post-doctoral fellow Psychology and NeuroCognition Lab (CNRS UMR 5105) Université Pierre Mendès-France BP47 --- 38040 Grenoble Cedex 9 --- France Email: matthieu.dub...@upmf-grenoble.fr Gmail: matth...@gmail.com http://web.upmf-grenoble.fr/LPNC/membre_matthieu_dubois [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] error using lapply with oneway_test (coin package)
That's a scoping problem, I think a bug in oneway_test. Because the formula var ~ group is created with the anonymous function within lapply, its environment should be the evaluation frame of that function call and var should be visible. If I replace oneway_test() with lm() it works. I think a workaround is to construct the data argument explicitly, i.e. lapply(y, function(var) oneway_test(var ~ group, data.frame(var=var, group=group))) yes, that would be the fix: R lapply(y, function(var) oneway_test(var ~ group, data = data.frame(var = var, group = group))) $V1 Asymptotic 2-Sample Permutation Test data: var by group (1, 2) Z = -1.2054, p-value = 0.2280 alternative hypothesis: true mu is not equal to 0 $V2 Asymptotic 2-Sample Permutation Test data: var by group (1, 2) Z = 0.5672, p-value = 0.5706 alternative hypothesis: true mu is not equal to 0 Thanks, Duncan. Torsten I've cc'd Torsten Hothorn, the maintainer of coin. Duncan Murdoch Thank you, Matthieu Matthieu Dubois Post-doctoral fellow Psychology and NeuroCognition Lab (CNRS UMR 5105) Université Pierre Mendès-France BP47 --- 38040 Grenoble Cedex 9 --- France Email: matthieu.dub...@upmf-grenoble.fr Gmail: matth...@gmail.com http://web.upmf-grenoble.fr/LPNC/membre_matthieu_dubois [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] Error with lapply [addirional clarification needed]
I need one more clarification here : Here I did : fn - function(i) return(list(i, i^2)) ss = sapply(1:4, fn) Here the object ss should be a matrix object : is.matrix(ss) However I feel it lacks some the matrix object properties. For example the syntax min(ss[1,]) generates an error : Error in min(ss[1, ]) : invalid 'type' (list) of argument. What should be the way out? Am I missing something ? Regards, baptiste auguie-2 wrote: Hi, you are feeding lapply i as an optional argument, which is passed to fn() and causes an error. Just use lapply(1:4, fn), or better yet, sapply, fn - function(i) return(i^2) sapply(1:4, fn) [1] 1 4 9 16 Hope this helps, baptiste On 20 Nov 2008, at 16:31, megh wrote: I have written following codes, with intention to get a list with values 1,2,9,16 : fn - function(i) return(i^2) lapply(1:4, fn, i) However I got following error : Error in FUN(1:4[[1L]], ...) : unused argument(s) (1) Can anyone please tell me what will be the correct code here? Regards, -- View this message in context: http://www.nabble.com/Error-with-lapply-tp20605066p20605066.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. _ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag __ R-help@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. -- View this message in context: http://www.nabble.com/Error-with-lapply-tp20605066p20634821.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Error with lapply [addirional clarification needed]
megh wrote: I need one more clarification here : Here I did : fn - function(i) return(list(i, i^2)) ss = sapply(1:4, fn) Here the object ss should be a matrix object : is.matrix(ss) However I feel it lacks some the matrix object properties. For example the syntax min(ss[1,]) generates an error : Error in min(ss[1, ]) : invalid 'type' (list) of argument. What should be the way out? Am I missing something ? ss is a dim'ed list object, because fn returns a list. This appears to be a feature, although rarely used. One point is that you can do fn - function(i) return(list(as.character(i), i^2)) ss - sapply(1:4, fn) ss [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 14916 ss[,1] [[1]] [1] 1 [[2]] [1] 1 I.e., the rows can be of different mode. The easiest way out is just not to do that, i.e. return a vectore c(i, i^2) instead. -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@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.
Re: [R] Error with lapply [addirional clarification needed]
Try str(ss) to see what it really looks like. You probably want: fn - function(i) c(i, i^2) On Sat, Nov 22, 2008 at 4:54 AM, megh [EMAIL PROTECTED] wrote: I need one more clarification here : Here I did : fn - function(i) return(list(i, i^2)) ss = sapply(1:4, fn) Here the object ss should be a matrix object : is.matrix(ss) However I feel it lacks some the matrix object properties. For example the syntax min(ss[1,]) generates an error : Error in min(ss[1, ]) : invalid 'type' (list) of argument. What should be the way out? Am I missing something ? Regards, baptiste auguie-2 wrote: Hi, you are feeding lapply i as an optional argument, which is passed to fn() and causes an error. Just use lapply(1:4, fn), or better yet, sapply, fn - function(i) return(i^2) sapply(1:4, fn) [1] 1 4 9 16 Hope this helps, baptiste On 20 Nov 2008, at 16:31, megh wrote: I have written following codes, with intention to get a list with values 1,2,9,16 : fn - function(i) return(i^2) lapply(1:4, fn, i) However I got following error : Error in FUN(1:4[[1L]], ...) : unused argument(s) (1) Can anyone please tell me what will be the correct code here? Regards, -- View this message in context: http://www.nabble.com/Error-with-lapply-tp20605066p20605066.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. _ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag __ R-help@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. -- View this message in context: http://www.nabble.com/Error-with-lapply-tp20605066p20634821.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
Re: [R] Error with lapply
Hi, you are feeding lapply i as an optional argument, which is passed to fn() and causes an error. Just use lapply(1:4, fn), or better yet, sapply, fn - function(i) return(i^2) sapply(1:4, fn) [1] 1 4 9 16 Hope this helps, baptiste On 20 Nov 2008, at 16:31, megh wrote: I have written following codes, with intention to get a list with values 1,2,9,16 : fn - function(i) return(i^2) lapply(1:4, fn, i) However I got following error : Error in FUN(1:4[[1L]], ...) : unused argument(s) (1) Can anyone please tell me what will be the correct code here? Regards, -- View this message in context: http://www.nabble.com/Error-with-lapply-tp20605066p20605066.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. _ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag __ R-help@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.
Re: [R] Error with lapply
on 11/20/2008 10:31 AM megh wrote: I have written following codes, with intention to get a list with values 1,2,9,16 : fn - function(i) return(i^2) lapply(1:4, fn, i) However I got following error : Error in FUN(1:4[[1L]], ...) : unused argument(s) (1) Can anyone please tell me what will be the correct code here? Regards, Try this: fn - function(i) i^2 lapply(1:4, fn) [[1]] [1] 1 [[2]] [1] 4 [[3]] [1] 9 [[4]] [1] 16 The error message indicates that the argument 'i' that you have in your initial attempt to use lappply() is unused, because the values 1:4 are passed to the function's first argument by default already. Thus, specifying 'i' again is an error, since there is not a second argument in your function fn(). Note also that 'return()' is not needed, as per the Details in ?return: If the end of a function is reached without calling return, the value of the last evaluated expression is returned. Note also, that since lapply() effectively uses an internal loop, a faster vectorized approach would be: as.list((1:4) ^ 2) [[1]] [1] 1 [[2]] [1] 4 [[3]] [1] 9 [[4]] [1] 16 For example, using 100,000 instead of 4: system.time(x1 - lapply(1:10, fn)) user system elapsed 0.500 0.015 0.600 system.time(x2 - as.list((1:10) ^ 2)) user system elapsed 0.018 0.004 0.039 identical(x1, x2) [1] TRUE HTH, Marc Schwartz __ R-help@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.
Re: [R] Error with lapply
lapply already passes the first arg to fn and by specifying the i (which is undefined -- its only defined within fn) it would be trying to to pass a second arg to fn yet fn takes only takes one arg. Try these: lapply(1:4, fn) lapply(1:4, ^, 2) On Thu, Nov 20, 2008 at 11:31 AM, megh [EMAIL PROTECTED] wrote: I have written following codes, with intention to get a list with values 1,2,9,16 : fn - function(i) return(i^2) lapply(1:4, fn, i) However I got following error : Error in FUN(1:4[[1L]], ...) : unused argument(s) (1) Can anyone please tell me what will be the correct code here? Regards, -- View this message in context: http://www.nabble.com/Error-with-lapply-tp20605066p20605066.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
Re: [R] Error with lapply
To be clear, the problem is not the return statement in your function, but the extra argument, i, in your lapply statement: lapply(1:4,fn) works just fine with your original function. You need to read ?lapply more carefully: fn receives the values of the first argument (1:4) in turn automatically; other arguments in lapply are passed in as **additional** arguments to fn, of which there are none here. Compare: fn - function(i,a)i^2+a lapply(1:4,fn) ## error lapply(1:4,fn,5) Cheers, Bert Bert Gunter Genentech -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Marc Schwartz Sent: Thursday, November 20, 2008 8:50 AM To: megh Cc: r-help@r-project.org Subject: Re: [R] Error with lapply on 11/20/2008 10:31 AM megh wrote: I have written following codes, with intention to get a list with values 1,2,9,16 : fn - function(i) return(i^2) lapply(1:4, fn, i) However I got following error : Error in FUN(1:4[[1L]], ...) : unused argument(s) (1) Can anyone please tell me what will be the correct code here? Regards, Try this: fn - function(i) i^2 lapply(1:4, fn) [[1]] [1] 1 [[2]] [1] 4 [[3]] [1] 9 [[4]] [1] 16 The error message indicates that the argument 'i' that you have in your initial attempt to use lappply() is unused, because the values 1:4 are passed to the function's first argument by default already. Thus, specifying 'i' again is an error, since there is not a second argument in your function fn(). Note also that 'return()' is not needed, as per the Details in ?return: If the end of a function is reached without calling return, the value of the last evaluated expression is returned. Note also, that since lapply() effectively uses an internal loop, a faster vectorized approach would be: as.list((1:4) ^ 2) [[1]] [1] 1 [[2]] [1] 4 [[3]] [1] 9 [[4]] [1] 16 For example, using 100,000 instead of 4: system.time(x1 - lapply(1:10, fn)) user system elapsed 0.500 0.015 0.600 system.time(x2 - as.list((1:10) ^ 2)) user system elapsed 0.018 0.004 0.039 identical(x1, x2) [1] TRUE HTH, Marc Schwartz __ R-help@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. __ R-help@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.