[R] Question about error of "non-numeric argument to binary operator"

Marc Schwartz MSchwartz at mn.rr.com
Wed Oct 11 05:04:45 CEST 2006


On Tue, 2006-10-10 at 22:35 -0400, Yulei Gong wrote:
> Hi,
> I have the following data and there is no binary operator contained,
> however, I still receive the error message when running unitrootTest
> function, could someone give me a guidance on it??
> 
> >readClipboard()
>   [1] "245"    "246"    "261.5"  "275.5"  "307"    "284.5"  "289"    "313.5"
> "323.75" "334"    " 312.5"  "325"    "305.5"  "322.5"  "317"    "310.5"
>  [17] "302"    "301"    "305"    "287"    "277.5"  "271"    " 271.5"  "278.5"
> "279"    "271"    "263"    "262"    "262"    "271"    "262"    "257"
>  [33] "251.5"  "258"    " 252.5"  "254.5"  "253"    "251.5"  "255"
> "253"    "253"    "243"    "238.5"  "234"    "229.5"  "230.5"  " 237.5"  "
> 235.5"
>  [49] "238"    "225"    "227.5"  "233"    "236.5"  "236.5"  "231.5"
> "225"    "221.5"  "221.5 "  "221.5"  "221.5"  "221.5"  "221.25" "221"
> "206"
>  [65] "207.5"  "196"    "192.5"  "193.5"  "186"    " 197.5"  "193.5"
> "201"    "195"    "183"    "185.5"  "181.5"  "179"    "177"    "173"    "
> 169.5"
>  [81] "173"    "178"    "169"    "173"    "167"    " 158.5"  "169.5"
> "164"    "145"    "127.5"  "132"    "131"    "131"    "120"    "120.5"  "
> 120.25"
>  [97] "120"    " 112.5"  "114.5"  "106"    "113"    "111"    "113"    "118.5"
> "131"    "131"    "146.5"  "133.5"  "128"    "132"    " 130.5"  "122"
> [113] "122.5"  "123.5"  "126"    "140"    "132"    "140"    "143"
> "148"    "168"    "162.5 "  "152.5"  "148"    "144"    "144"    "150.5"
> "151"
> [129] "156"    "156"    "156"    "152"    "156"    " 153.5"  "137"
> "135"    "140"    "135"    "138.5"  "139"    "130"    "131"    "125"
> "125"
> [145] "121.5 "  "125.5"  "128"    "128"    "129.5"  "133"    "129.5"
> "140"    "154.5"  "167.5"  "156"    "179"    " 178.5"  "174"    "188"
> "214"
> [161] "197.5"  "181.5"  "181.5"  "197.5"  "191.5"  "179"    "189.5"
> "184.5"  "183"    "182"    "
> 182.5"  "177"    "191"    "198"    "191"    "180.5"
> [177] "182"    "183.5"  "183"    "182"    " 189.5"  "195"    "208"
> "203"    "194"    "176.5"  "173"    "173"    "174"    "165.5"  "163"    "
> 162.5"
> [193] "159"    "162.5"  "171"    "168.5"  "164"    "158"    "147"
> "149"    "149.5"  "144"    "141"    " 138.5"  "138"    "136"    "138"
> "140"
> [209] "135"    "132.5"  "130.75" "129"    "129.5"  "126"    "127"    " 128.5"
> "127.5"  "124"    "117"    "119"    "120.5"  "122"    "129"    "133"
> [225] "136"    "137"    "133"    "133"    "127"    "123"    "122"
> "117"    "122"    "126"    "126"    "133"    " 127.5"  "129"    "130"
> "125"
> [241] "122"    "126.5"  "136"    "148"    "147"    "150.5"  "143.5"
> "138.5"  "134"    "135"    "
> 135.75" "136.5"  "132"    "129"    "127.5"  "118.5"
> > x<-readClipboard()
> > unitrootTest(x)
> Error in r[i1] - r[-length(r):-(length(r) - lag + 1)] :
>         non-numeric argument to binary operator
> 
> I also try to do simple code with it, and getting error message as
> well, such as
> >for(j in 1:length(x)){w<-x/1}
> >Error in x/1 : non-numeric argument to binary operator
> 
> Thanks for your help!
> 
> Yulei

Your 'x' is a character vector, not numeric. The tip is that the values
are surrounded by double quotes.  Thus, you are trying to use operators
intended for numerics on characters.

For example:

> x <- c("122", "126.5", "136", "148", "147", "150.5", "143.5")

> x
[1] "122"   "126.5" "136"   "148"   "147"   "150.5" "143.5"

> x / 1
Error in x/1 : non-numeric argument to binary operator

> is.numeric(x)
[1] FALSE

> is.character(x)
[1] TRUE


It's not clear from your post how you read in the data originally.  You
should review that process or alternatively, coerce 'x' to numeric:

> x.num <- as.numeric(x)

> x.num
[1] 122.0 126.5 136.0 148.0 147.0 150.5 143.5

> x.num / 1
[1] 122.0 126.5 136.0 148.0 147.0 150.5 143.5


HTH,

Marc Schwartz



More information about the R-help mailing list