I am writing the code that will identify what category does an entry in a table (target table) belongs to, comparing that entry to another table (code table).
For example, the code table is:
df_code <- data.frame("Colour" = c("Blue", "Red", "Yellow"), "Shape" = c("Triangle", "Circle", "Square"), "Letter" = c("Alpha", "Beta", "Charlie"))
The column headers are the names of the categories, and the entries in each column are what should be classified as specified category in the target table.
The target table is then:
df_target <- data.frame("ID" = c(1:3), "Label" = c("Red", "Triangle", "Beta"))
I also have a function that finds an entry from the target table in the code table:
findInCategory <- function (code = df_code, entry) {
entry.location <- which(code == entry, arr.ind = T)
category <- colnames(code)[entry.location[2]]
return(category)
}
What I need to do is use this function together with group_by(Label)
and mutate()
from dplyr
package to create the "Category" column in the df_target
, assigning the appropriate category to each row. Is there a way to pass the current group of entries as an argument for my function like this:
df_target_mutated <- df_target %>% mutate(metacategory = findInCategory(code, **An argument here**))
The df_target_mutated
should look like this after the transformation:
ID Label Category
1 1 Red Colour
2 2 Triangle Shape
3 3 Beta Letter