0

I have some trouble while trying to get a subset of an initial dataframe as in the end it corrupts my dataframe! Here is an example of what is happening:

Let consider a dataframe :

>test=data.frame("v1"=c(1,2,3,4,-5,-3),"v2"=c(1,2,3,4,5,6))
> print(test)
  v1 v2
1  1  1
2  2  2
3  3  3
4  4  4
5 -5  5
6 -3  6

Then I want to take the subset where var1 value is strictly below, let's say -2:

> subtest=test[test$v1<-2,]
> print(subtest)
  v1 v2
2  2  2
> print(test)
  v1 v2
1  2  1
2  2  2
3  2  3
4  2  4
5  2  5
6  2  6

Not only did the subset operation didn't work but it actually corrupt my initial database by replacing all the v1 values by 2 !

MickMack
  • 3
  • 1

2 Answers2

3

It is not a bug. You just have to put brackets, because otherwise it will be assignment mark <-. If you want strictly below -2:

subtest=test[test$v1<(-2),]
adomasb
  • 496
  • 4
  • 14
3

I t is not a bug. It is just a typo!

The operator <- is used to assign values. In the command subtest=test[test$v1<-2,], you have assigned all values of variable v1 to 2.

To do what you want, you should do that instead:

subtest=test[test$v1< -2,]
Pop
  • 12,135
  • 5
  • 55
  • 68