Filtering a data frame

后端 未结 3 1654
我寻月下人不归
我寻月下人不归 2020-12-29 12:36

I have read in a csv file in matrix form (having m rows and n columns). I want to filter the matrix by conducting a filter in verbal form:

Select all values from co

3条回答
  •  星月不相逢
    2020-12-29 13:20

    You said you just wanted the column x values where column_values was 15, right?

    subset(dat, column_values==15, select=x)
    

    I think this may come as a dataframe so it's possble you may need to unlist() it and maybe even "unfactor" it.

    > dat
      Subject Product
    1       1   ProdA
    2       1   ProdB
    3       1   ProdC
    4       2   ProdB
    5       2   ProdC
    6       2   ProdD
    7       3   ProdA
    8       3   ProdB
    > subset(dat, Subject==2, Product)
      Product
    4   ProdB
    5   ProdC
    6   ProdD
    > unlist( subset(dat, Subject==2, Product) )
    Product1 Product2 Product3 
       ProdB    ProdC    ProdD 
    Levels: ProdA ProdB ProdC ProdD
    > as.character( unlist( subset(dat, Subject==2, Product) ) )
    [1] "ProdB" "ProdC" "ProdD"
    

    If you want all of the columns you can drop the third argument (the select= argument):

    subset(dat, Subject==2 )
    
      Subject Product
    4       2   ProdB
    5       2   ProdC
    6       2   ProdD
    

提交回复
热议问题