I'd like to run a function on an score column based on another column of type character
. I want to scale this function, so it can get the name of the column and give an array with the results for each element.
Currently, I'm doing it manually, meaning I see what the unique values are, then run the function, and finally append the outputs. See simple example below:
#data
df<-data.frame(category1 = sample(c("M", "F"),100,replace=TRUE),
category2 = sample(c("A", "B", "C"),100,replace=TRUE),
score = runif(100))
#identifying the elements
table(df$category1)
# F M
#43 57
#running the function
append(
sum(df$score[df$category1 == 'M']),
sum(df$score[df$category1 == 'F'])
)
Ideally, I'd have this:
f <- function(dataframe, score_Column, categoriaal_column){
identify the levels
run the function for each level
create a table with each level and its corresponding output
}