I have a strange issue with subset: it doesn’t recognize the variables in my dataframe.
For example, if I want a summary of the variable v1 in the dataframe DF, I get:
summary(DF$v1)
Min. 1st Qu. Median Mean 3rd Qu. Max
-123.00 4.00 14.00 84.62 49.00 13140.00
Now, this variable should always be non-negative, so I want to inspect the negative values by subsetting the dataframe DF into the dataframe PROBS. But if I try this:
PROB <- subset[DF, v1 < 0]
I get:
Error: object "v1" not found
I am positive that I am not misspelling this variable name. I have tried with other variables in this dataframe, but it keeps throwing this error at me.
Does anyone have any idea about the reason for such a strange behaviour? I have never had any problem with the subset function!
Thanks.
Subset is a function, so you should use
subset(DF, v1 < 0)instead of the square brackets.Square brackets are used for subsetting dataframes or matrices directly.
subset[DF, v1 < 0]is trying to subset thesubsetfunction by looking for the variablev1within the scope of thesubsetobject. The objectsubsetis a function, so it can’t find thev1object.