Reputation: 378
I have a dataframe a
with row names. The row names are unique string names, something like this:
order..new..i...2.ncol.new..i.....
J.TYMO 620
J.TTMO 2851
J.NTT 1972
J.ABOT 565
J.NNDO 1025
J.SFTB 2509
when I execute a one-liner code as.data.frame(a[a[1] <= 2000])
to remove rows less than or equal to a value (as in 2000) my result does not include the row names.
I would EXPECT my code to do something like this:
J.TYMO 620
J.NTT 1972
J.ABOT 565
J.NNDO 1025
Instead it does this:
1 620
2 1972
3 565
4 1025
Any ideas? Thanks.
Upvotes: 2
Views: 212
Reputation: 1763
EDIT I reproduced your error, you need to add the drop = FALSE
option in your subsetting to get a data.frame as result and not a vector :
df_a <- structure(list(order..new..i...2.ncol.new..i..... = c(620L, 2851L, 1972L, 565L, 1025L, 2509L)), row.names = c("J.TYMO", "J.TTMO", "J.NTT", "J.ABOT", "J.NNDO", "J.SFTB"), class = "data.frame")
str(df_a)
#> 'data.frame': 6 obs. of 1 variable:
#> $ order..new..i...2.ncol.new..i.....: int 620 2851 1972 565 1025 2509
names(df_a) <- "V1"
df_a[df_a[[1]] <= 1000 , , drop = FALSE]
#> V1
#> J.TYMO 620
#> J.ABOT 565
OLD ANSWER
The best with-row-names-dataset I though of was the mtcars
dataset. Building from that I found that adding a comma in your call solves the problem :
dfr <- head(mtcars)
dfr
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
as.data.frame(dfr[dfr[1]<20 , ])
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
Thus with your peculiar a
object, if it is a data.frame the answer should be :
as.data.frame(a[a[1] <= 333 , ])
Upvotes: 3
Reputation:
You're extracting one variable as a vector and then converting that vector into a data frame. You should just use a[a[1] <= 333,]
or subset(a, blah <= 333)
to subset your dataframe. Example:
a <- `row.names<-`(data.frame(blah = 332:335, bleh = "bleh"),
c("row1", "row2", "row3", "row4")
)
#### OUTPUT ####
blah bleh
row1 332 bleh
row2 333 bleh
row3 334 bleh
row4 335 bleh
Now subset with a[a[1] <= 333,]
or subset(a, blah <= 333)
:
blah bleh
row1 332 bleh
row2 333 bleh
If you're trying to remove rows less than or equal to a value (as in 333) then you should instead use a[a[1] > 333,]
or subset(a, blah > 333)
:
blah bleh
row3 334 bleh
row4 335 bleh
Upvotes: 1