I am facing an issue while subsetting a data frame in R. Data frame is att2 which has a column filter_name based upon which I want to subset. The unique values for this column are below.
unique(att2[["filter_name"]])
# [1] title Type Operating_System Occasion Brand
148 Levels: Accessories Age Antennae Art_Style Aspect_ratio ... Zoom
This shows that Brand is a value for filter_name column. But when I subset the frame using below code, it gives 0 rows as below.
att3 <- subset(att2, filter_name == 'Brand')
> att3
[1] a b c filter_name
<0 rows> (or 0-length row.names)
I am not able to find out the reason. Has anyone faced this kind of issue?
All that we can do is guess at what the source of your problem might be.
Here’s my best guess: Your “filter_name” column has whitespace in it, thus you shouldn’t actually be looking for “Brand” until you strip the whitespace.
Here’s a minimal example that reproduces your problem if my guess is correct:
First, some sample data:
Use
printwith thequote = TRUEargument to see the whitespace in yourdata.frame:If that happens to be your problem, then a quick
gsubshould fix it:You may also want to look into the
strip.whiteargument inread.tableand family which defaults toFALSE. Try re-reading in your data withstrip.white = TRUEand then try your subsetting.