[R] Error non-numeric argument to binary operator
Uwe Ligges
ligges at statistik.tu-dortmund.de
Wed Jul 13 19:38:04 CEST 2011
On 13.07.2011 17:46, Bansal, Vikas wrote:
> Dear all,
>
> I have a data frame df and i am using a code shown below.
>
> df[, "total"]<- rowSums(df[, 3:6]) MAKING A NEW COLUMN total in df BY ROWSUMS
>
> defineSamples<- function()
> {
> readline("enter the number of INDIVIDUALS IN POOL: ")
> }
> df$ind=definesamples() CALLING THE FUNCTION AND STORING THE INPUT FROM USER IN NEW COLUMN OF df
This won't work, because
1. defineSamples != definesamples
2. readline returns a character but you expect a numeric value - which
is why you get the error message. Just coerce to numeric and implement a
careful check if the entered value can be coerced successfully.
Uwe Ligges
> df[, 3:6]<- apply(df[, 3:6], 2, function(x) x / df[, "total"] * df[, "ind"] * 2) APPLYING A MATHEMATICAL FORMULA
>
> I am taking an input from user which is stored in ind column of df.and then i am applying a formula showed in last line of code.
> But it shows this error-
>
> Error in x/df[, "total"] * df[, "ind"] :
> non-numeric argument to binary operator
>
> but if i am not taking input from user and using straight way this-
> df$ind=9
> then it is working.
>
> But i want to enter the value of ind from user.Can you please tell me how can i remove this error.
> Thanking you,
> Warm Regards
> Vikas Bansal
> Msc Bioinformatics
> Kings College London
> ______________________________________________
> 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