This is a problem with using attach()
, which is not recommended in R - for this very reason! The problem is that your code is somewhat ambiguous, or at least it is something else than you expected.
How to resolve this?
detach
dataset and- do not use
attach
again. Use [
and / or $
instead, and if you like with()
for a subset of your data.
Here's how you could do it for an example:
detach(mtcars) ord.cars <- mtcars[order(mtcars$hp),] sub.cars <- subset(mtcars, hp > 120) #the subset could also be written as: sub.cars <- mtcars[mtcars$hp > 120,] ord.sub <- sub.cars[order(sub.cars$mpg),] head(ord.sub) # only show the first 6 rows mpg cyl disp hp drat wt qsec vs am gear carb Cadillac Fleetwood 10.4 8 472 205 2.93 5.25 18.0 0 0 3 4 Lincoln Continental 10.4 8 460 215 3.00 5.42 17.8 0 0 3 4 Camaro Z28 13.3 8 350 245 3.73 3.84 15.4 0 0 3 4 Duster 360 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 Chrysler Imperial 14.7 8 440 230 3.23 5.34 17.4 0 0 3 4 Maserati Bora 15.0 8 301 335 3.54 3.57 14.6 0 1 5 8
What exactly caused the problem in your code?
After you attached
mtcars data, whenever you call one of the nested data column names, for example mpg
, it will refer to the attached data set (the original mtcats data). Then the problem was that you multiplied the data and saved it in a new object (sub.cars) that was not attached while mtcars was still attached. Then, when you tried to order sub.cars data, you used sub.cars[order(mpg),]
, and, as you can see, there you are referring to the mpg
column, which is interpreted by R as one of the supplied (original) mtcars collection data with more rows than a subset of the data. All of these lines in your sub.cars that have been excluded by the subset will now display as NA in sub.cars
.
Lesson: do not use attach()
.
source share