[R] For is.numeric condition in user input

Bansal, Vikas vikas.bansal at kcl.ac.uk
Mon Jul 25 14:27:23 CEST 2011


I have tried this,and it is working.But the thing is if user will input a character rather than number,in the end it is showing a warning message-

Warning messages:
1: NAs introduced by coercion 

and this is not good for my tool as I do not want to show this warning to the user.



Typo on my part; should be 'is.na':

so you would do:

repeat{
    value <- as.numeric(readline())
    if (!is.na(value)) break
}

On Mon, Jul 25, 2011 at 8:05 AM, Bansal, Vikas <vikas.bansal at kcl.ac.uk> wrote:
> Thanks for your reply.But I have never seen ia.na in R.Can you please tell me how to use this?
> So you are saying rather than is.numeric,I have to test user input by ia.na?
>
>
> Thanking you,
> Warm Regards
> Vikas Bansal
> Msc Bioinformatics
> Kings College London
> ________________________________________
> From: jim holtman [jholtman at gmail.com]
> Sent: Monday, July 25, 2011 12:58 PM
> To: Bansal, Vikas
> Cc: Smart Guy; Ista Zahn; r-help at r-project.org
> Subject: Re: [R] For is.numeric condition in user input
>
> If you run a simple test (that is what is nice about R being
> interpreted), you will see that 'as.numeric' is TRUE; what you want to
> test for is 'ia.na':
>
>> a
> [1] "12as"
>> as.numeric(a)
> [1] NA
> Warning message:
> NAs introduced by coercion
>> is.numeric(as.numeric(a))
> [1] TRUE
> Warning message:
> NAs introduced by coercion
>
>
>
> On Mon, Jul 25, 2011 at 7:49 AM, Bansal, Vikas <vikas.bansal at kcl.ac.uk> wrote:
>> Thanks for your reply.I know readline will give me a character.But if I will do something like this-
>>
>>
>>> readnumber<- function()
>>>  {
>>> for(j in 1:10){
>>>  value=readline("enter the threshold for number of reads: ")
>>> value=as.numeric(value)
>>>  if(is.numeric(value)==T)
>>> {return(value)
>>> break}
>>> else
>>> print("wrong number Please enter numerical value ")}
>>>
>>>  }
>>
>> if i will change value as numeric and if now user will input a character like a or b rather than a number like 4 or 5 or 6,then my code is not showing message-
>> wrong number Please enter numerical value
>>
>> That is why I am confused now-I have tried with- value=as.numeric(value)
>> and without this also.But did not find any solution.
>>
>>
>> Thanking you,
>> Warm Regards
>> Vikas Bansal
>> Msc Bioinformatics
>> Kings College London
>> ________________________________________
>> From: Smart Guy [smartguy3k at gmail.com]
>> Sent: Monday, July 25, 2011 6:39 AM
>> To: Ista Zahn
>> Cc: Bansal, Vikas; r-help at r-project.org
>> Subject: Re: [R] For is.numeric condition in user input
>>
>> Yes, thats right, readline will give you character and now you need to convert it to numeric to make it work.
>>
>> Thanks
>> SmartG
>>
>> On 25 July 2011 08:51, Ista Zahn <izahn at psych.rochester.edu<mailto:izahn at psych.rochester.edu>> wrote:
>> readline always returns a character. See ?readline for details.
>>
>> Best,
>> Ista
>>
>> On Sun, Jul 24, 2011 at 10:59 PM, Bansal, Vikas <vikas.bansal at kcl.ac.uk<mailto:vikas.bansal at kcl.ac.uk>> wrote:
>>> Dear all,
>>>
>>> I am using the following function so that user can input a numerical value.
>>>
>>> readnumber<- function()
>>>  {
>>> for(j in 1:10){
>>>  value=readline("enter the threshold for number of reads: ")
>>>  if(is.numeric(value)==T)
>>> {return(value)
>>> break}
>>> else
>>> print("wrong number Please enter numerical value ")}
>>>
>>>  }
>>>
>>> But if by chance user tries to put character it will show the message-
>>>  wrong number Please enter numerical value
>>>
>>> now when I am calling this function and entering numerical value,then also it is showing the message-wrong number Please enter numerical value
>>>
>>> Can you please tell me what mistake I am doing?
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> Thanking you,
>>> Warm Regards
>>> Vikas Bansal
>>> Msc Bioinformatics
>>> Kings College London
>>> ______________________________________________
>>> R-help at r-project.org<mailto: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.
>>>
>>
>>
>>
>> --
>> Ista Zahn
>> Graduate student
>> University of Rochester
>> Department of Clinical and Social Psychology
>> http://yourpsyche.org
>>
>> ______________________________________________
>> R-help at r-project.org<mailto: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.
>>
>>
>>
>> --
>> SmartG
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list