rdplyrdata-filtering

Data Filter based on object and variable using R


I am trying to filter objects from variables. here is my data;

head(DfUse)
InstanceType  ProductDescription      SpotPrice       ymd_hms(Timestamp)
<chr>                 <chr>           <dbl>                    <chr> 
1   a1.2xlarge  Linux/UNIX  0.0671  06:17:23
2   a1.2xlarge  Red Hat Enterprise Linux    0.1971  06:17:23
3   a1.2xlarge  SUSE Linux  0.2171  06:17:23
4   a1.4xlarge  Linux/UNIX  0.1343  12:15:54
5   a1.4xlarge  Red Hat Enterprise Linux    0.2643  12:15:54
6   a1.4xlarge  SUSE Linux  0.2843  12:15:54

dimension of data is

dim(DfUse)
[1] 10078     4

data set structure

str(DfUse)
'data.frame':   10078 obs. of  4 variables:
 $ InstanceType      : chr  "  a1.2xlarge" "  a1.2xlarge" "  a1.2xlarge" "  a1.4xlarge" ...
 $ ProductDescription: chr  "  Linux/UNIX" "  Red Hat Enterprise Linux" "  SUSE Linux" "  Linux/UNIX" ...
 $ SpotPrice         : num  0.0671 0.1971 0.2171 0.1343 0.2643 ...
 $ ymd_hms(Timestamp): chr  "06:17:23" "06:17:23" "06:17:23" "12:15:54" ...

when i try to filter

filter(DfUse, InstanceType == 'a1.2xlarge')
0 rows

Kindly help in filtering the data set. I want to stack all filtered values group by other variables.


Solution

  • From the dataset structure it seems you have some whitespace in your data. You can use trimws to remove it.

    dplyr::filter(DfUse, trimws(InstanceType) == 'a1.2xlarge')
    

    With base R subset -

    subset(DfUse, trimws(InstanceType) == 'a1.2xlarge')