Re: [R] Error:non-numeric argument in my function
Stavros Macrakis wrote: I agree that it's inconsistent that 1:'2' -- 1:2 # this doesn't seem to be documented in ? seq 1+ '2' -- error 1+factor(2) -- NA (with a warning) 1 : factor(4) -- 1 (uses as.numeric/unclass of factor) ...i'd expect ...a successful computation (with a character - numeric coercion of '2' to 2)... But I disagree that the best thing to do here is to have them all coerce to numerics. In scripting languages like Perl which are constantly going back and forth between internal and external representations, it makes some sense to auto-convert numeric strings to numerics and vice versa, but I don't think it makes sense in a statistical language. I would rather see them all give errors. you're right; it actually seems to be a *bug* that 1:'2' or '1':2 successfully return. ?':' says: The binary operator ':' has two meanings: for factors 'a:b' is equivalent to 'interaction(a, b)' (but the levels are ordered and labelled differently). For numeric arguments 'from:to' is equivalent to 'seq(from, to)', and generates a sequence from 'from' to 'to' in steps of '1' or '1-'. but '1' and '2' are neither factors nor numerics, and ':' should have no meaning for them. interestingly, 0i:1 # 0 1 '0':1 # 0 1 i.e., both the complex 0i and the character '0' are successfully (but undocumentedly) coerced to integers, but '0i':1 # Error in 0i:1 : NA/NaN argument # In addition: Warning message: # NAs introduced by coercion while it does not follow from ?':' that the arguments are implicitly coerced to numerics, it seems a plausible explanation: as.numeric(0i) # 0 as.numeric('0') # 0 as.numeric('0i') # NA interestingly, as.complex('0i') # NA ?as.complex does not explain what sorts of arguments are coercible to complex, beyond an object, probably of mode 'complex'. if as.numeric can coerce strings to numerics, why can't as.complex do the same for complex numbers? vQ __ 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:non-numeric argument in my function
I thought Stavros' suggestion was going to be to have the error message say what type of offending object was found. If the message said that a list of class 'data.frame' was found (probably the leading case), then that would be much more helpful. Patrick Burns patr...@burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of The R Inferno and A Guide for the Unwilling S User) Stavros Macrakis wrote: On Sun, May 31, 2009 at 6:10 PM, jim holtman jholt...@gmail.com wrote: Message is very clear: 1 * 'a' Error in 1 * a : non-numeric argument to binary operator Though the user should have been able to figure this out, perhaps the error message could be improved? After all, it is not the fact that the operator is *binary* that implies that its argument must be numeric, but that it is *arithmetic*. The binary operator %in%, for example, takes non-numeric arguments. Suggested replacement error message: non-numeric argument to arithmetic operator -s [[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:non-numeric argument in my function
Agreed, that's even better, e.g. Error in 1 * a : character argument not allowed for arithmetic operator * For some reason (does anyone know the rationale?), in the case of factors, you don't get an error, but a more explicit warning and an NA result: 2*factor(3) [1] NA Warning message: In Ops.factor(2, factor(3)) : * not meaningful for factors This seems hazardous, especially since the user has to be sophisticated enough to know about options(warn=2) to get a traceback for this. As for data frames, arithmetic operators seem to work if all the values are numeric: 2*data.frame(a=1) a 1 2 It's a hard problem to make useful error messages for beginning users -s On Mon, Jun 1, 2009 at 4:34 AM, Patrick Burns pbu...@pburns.seanet.comwrote: I thought Stavros' suggestion was going to be to have the error message say what type of offending object was found. If the message said that a list of class 'data.frame' was found (probably the leading case), then that would be much more helpful. Patrick Burns patr...@burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of The R Inferno and A Guide for the Unwilling S User) Stavros Macrakis wrote: On Sun, May 31, 2009 at 6:10 PM, jim holtman jholt...@gmail.com wrote: Message is very clear: 1 * 'a' Error in 1 * a : non-numeric argument to binary operator Though the user should have been able to figure this out, perhaps the error message could be improved? After all, it is not the fact that the operator is *binary* that implies that its argument must be numeric, but that it is *arithmetic*. The binary operator %in%, for example, takes non-numeric arguments. Suggested replacement error message: non-numeric argument to arithmetic operator -s [[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. [[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.
Re: [R] Error:non-numeric argument in my function
i think the error message might be even better, but this would require '* to be even better. i know some will take it for lamenting: there is an ugly lack of consistency here: 1:2 # 1 2 1:2.5 # 1 2 (coercion double - integer) 1:'2' # 1 2 (corecion character - integer) 1:'a' # Error in 1:a : NA/NaN argument # In addition: Warning message: # NAs introduced by coercion 1 * 2 # 2 1 * '2' # Error in 1 * 2 : non-numeric argument to binary operator 1 * '2' # Error in 1 * a : non-numeric argument to binary operator in the two last cases, i'd expect, respectively, a successful computation (with a character - numeric coercion of '2' to 2), and an error similar to the one for 1:'a'. the current error message isn't good, and what pat and stavros propose seems much better; i think it could be even better if 1 * '2' worked, and if 1 * 'a' caused an error of the form cannot coerce 'a' to numeric for use with the arithmetic operator '*'. vQ Stavros Macrakis wrote: Agreed, that's even better, e.g. Error in 1 * a : character argument not allowed for arithmetic operator * For some reason (does anyone know the rationale?), in the case of factors, you don't get an error, but a more explicit warning and an NA result: 2*factor(3) [1] NA Warning message: In Ops.factor(2, factor(3)) : * not meaningful for factors This seems hazardous, especially since the user has to be sophisticated enough to know about options(warn=2) to get a traceback for this. As for data frames, arithmetic operators seem to work if all the values are numeric: 2*data.frame(a=1) a 1 2 It's a hard problem to make useful error messages for beginning users -s On Mon, Jun 1, 2009 at 4:34 AM, Patrick Burns pbu...@pburns.seanet.comwrote: I thought Stavros' suggestion was going to be to have the error message say what type of offending object was found. If the message said that a list of class 'data.frame' was found (probably the leading case), then that would be much more helpful. Patrick Burns patr...@burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of The R Inferno and A Guide for the Unwilling S User) Stavros Macrakis wrote: On Sun, May 31, 2009 at 6:10 PM, jim holtman jholt...@gmail.com wrote: Message is very clear: 1 * 'a' Error in 1 * a : non-numeric argument to binary operator Though the user should have been able to figure this out, perhaps the error message could be improved? After all, it is not the fact that the operator is *binary* that implies that its argument must be numeric, but that it is *arithmetic*. The binary operator %in%, for example, takes non-numeric arguments. Suggested replacement error message: non-numeric argument to arithmetic operator -s __ 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] Error:non-numeric argument in my function
Hello! I have a function: zywnoscCalosc- function( jedzenie, n1, n2, n3, n4, d1, d2, d3, d4 ) { ndf - data.frame(nn1=n1,nn2=n2,nn3=n3,nn4=n4) ddf - data.frame(dd1=d1,dd2=d2,dd3=d3,dd4=d4) for (i in 1:length(n1)){ wekt_n = ndf[i,] wekt_n_ok = wekt_n[!is.na(wekt_n)] dl_n = length(wekt_n_ok) wynik = (1*wekt_n_ok)/(1*dl_n) } } and I get an error like this: Error in 1 * wekt_n_ok : non-numeric argument to binary operator Anybody can help me? -- View this message in context: http://www.nabble.com/Error%3Anon-numeric-argument-in-my-function-tp23807218p23807218.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:non-numeric argument in my function
Message is very clear: 1 * 'a' Error in 1 * a : non-numeric argument to binary operator wekt_n_ok must be non-numeric. You need to provide a reproducible script. You also need to learn about debugging. 'str(wekt_n_ok )' when the error occurred may have helped to pinpoint the problem. On Sun, May 31, 2009 at 5:43 PM, Grze¶ gregori...@gmail.com wrote: Hello! I have a function: zywnoscCalosc- function( jedzenie, n1, n2, n3, n4, d1, d2, d3, d4 ) { ndf - data.frame(nn1=n1,nn2=n2,nn3=n3,nn4=n4) ddf - data.frame(dd1=d1,dd2=d2,dd3=d3,dd4=d4) for (i in 1:length(n1)){ wekt_n = ndf[i,] wekt_n_ok = wekt_n[!is.na(wekt_n)] dl_n = length(wekt_n_ok) wynik = (1*wekt_n_ok)/(1*dl_n) } } and I get an error like this: Error in 1 * wekt_n_ok : non-numeric argument to binary operator Anybody can help me? -- View this message in context: http://www.nabble.com/Error%3Anon-numeric-argument-in-my-function-tp23807218p23807218.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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[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.
Re: [R] Error:non-numeric argument in my function
On Sun, May 31, 2009 at 6:10 PM, jim holtman jholt...@gmail.com wrote: Message is very clear: 1 * 'a' Error in 1 * a : non-numeric argument to binary operator Though the user should have been able to figure this out, perhaps the error message could be improved? After all, it is not the fact that the operator is *binary* that implies that its argument must be numeric, but that it is *arithmetic*. The binary operator %in%, for example, takes non-numeric arguments. Suggested replacement error message: non-numeric argument to arithmetic operator -s [[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.
Re: [R] Error:non-numeric argument in my function
Thanks jholtman! But I'm not sure what and where I should change my code... :( wekt_n = ndf[i,] wekt_n_ok = wekt_n[!is.na(wekt_n)] If before this line I should change wekt_n_ok as numeric? but if I wrote as.numeric(wekt_n_ok) it's also not correct and I also don't understand why wekt_n_ok is not numeric? -- View this message in context: http://www.nabble.com/Error%3Anon-numeric-argument-in-my-function-tp23807218p23807873.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:non-numeric argument in my function
What you need to do is to see that is in 'wekt_n_ok' at that point in your program. You should be able to see it with str(wekt_n_ok) when the error occurs. You can also add a print statement in the loop to print out the value before it is used with the binary operator. It would help if you provided 'str' of the various objects you are using, or maybe some reproducible code. Somehow you are creating a non-numeric value in your code; you need to debug it and check all the values before you use them. On Sun, May 31, 2009 at 7:05 PM, Grze¶ gregori...@gmail.com wrote: Thanks jholtman! But I'm not sure what and where I should change my code... :( wekt_n = ndf[i,] wekt_n_ok = wekt_n[!is.na(wekt_n)] If before this line I should change wekt_n_ok as numeric? but if I wrote as.numeric(wekt_n_ok) it's also not correct and I also don't understand why wekt_n_ok is not numeric? -- View this message in context: http://www.nabble.com/Error%3Anon-numeric-argument-in-my-function-tp23807218p23807873.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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[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.