Search code examples
rvectorcountgroupingcategories

Can I use R to get a category count for every time an item in a vector pertains to a certain category?


I am a novice R user hoping for some help:

I want to write some code where I can input a vector and determine how many times each item fell within a category. For example, if I had three categories in a data frame and a vector in question:

```
fruits<-c("Apple","Banana","Pear")
vegetables<-c("Broccoli","Spinach","Peas")
flowers<-c("Rose","Daisy","lily")

df<-cbind(fruits,vegetables,flowers)

df

vectorinquestion<-c("Banana","Peas","Apple")
```

What kind of code can I use to get the output of:

Fruit = 2
Vegetable = 1
Flower = 0

Any help with this would be greatly appreciated! I am trying to learn new functions, so this would help a lot.


Solution

  • You can loop apply %in%, creating a logical matrix.

    
    logical_matrix<-sapply(df, function(x) vectorinquestion %in% x)
    > logical_matrix
         fruits vegetables flowers
    [1,]   TRUE      FALSE   FALSE
    [2,]  FALSE       TRUE   FALSE
    [3,]   TRUE      FALSE   FALSE
    

    Then you can loop apply a function to sum your TRUEs for every column:

    > colSums(logical_matrix)
        fruits vegetables    flowers 
             2          1          0 
    

    All in a one-liner:

    colSums(sapply(df, function(x) vectorinquestion %in% x)
    

    With purrr:

    df%>%map(~vectorinquestion %in% .)%>%cbind.data.frame()%>%colSums()