Reputation: 285
I was trying to replace values >= 4 in column cnt1 with ">4"
date cnt1 cnt2 cnt3 cnt4
2014-01 3 0 0 1
2014-01 5 0 0 2
2014-01 8 3 0 4
2014-01 32 29 0 30
2014-01 3 3 0 3
2014-01 1 1 0 1
2014-01 1 1 0 1
My code goes like this:
output$cnt1[output$cnt1>= "4"] <- ">=4"
And the result is like below:
date cnt1 cnt2 cnt3 cnt4
2014-01 3 0 0 1
2014-01 >4 0 0 2
2014-01 >4 3 0 4
2014-01 32 29 0 30
2014-01 3 3 0 3
2014-01 1 1 0 1
2014-01 1 1 0 1
So it turns out that some values were not replaced.... Did I do something wrong? The Str of the data:
$ cnt1: chr "1" "1" "2" "2" ...
$ cnt2: chr "1" "1" "1" "0" ...
$ cnt3: chr "0" "0" "0" "0" ...
$ cnt4: int 1 1 1 1 1 1 5 4 1 1 ...
Thanks
Upvotes: 0
Views: 100
Reputation: 1384
As @thelatemail pointed out in comments you aren't comparing the same thing
Imagine that instead of comparing comething as being greater than 4, you compared something as being greater than "blah"
Is 5 > "blah" ? (is an integer greater than a string)
You want to compare integers with integers.
output$cnt1[output$cnt1 >= 4]
Not
output$cnt1[output$cnt1>= "4"]
As well as that, when you replace the values with ">4" all the other values in that column will be converted from a numeric type to a character type. This may or may not be what you are wanting
When comparing strings you will get peculiar results. R does comparison this way whereby it looks at each character converts it to it's asci values, and then compares ascii value by ascii value
Consider:
> "a">"b"
[1] FALSE
>
> "b"> "a"
[1] TRUE
>
> "3">"4"
[1] FALSE
>
> "5">"4"
[1] TRUE
>
> "53">"4"
[1] TRUE
>
> "35">"4"
[1] FALSE
> "alpha">"alphb"
[1] FALSE
If you translate each expressions into their corresponding ascii values; it all follows
Upvotes: 2