1

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
}
Ana
  • 1,516
  • 3
  • 15
  • 26
  • 1
    Do you need `library(dplyr);df %>% group_by(category1, category2) %>% mutate(n = n())` – akrun Oct 30 '18 at 00:26
  • you're right! I guess I over complicated it in my mind! I did this based on your comment: `library(dplyr);df %>% group_by(category1) %>% transmute(n = n()) %>% unique() %>% as.data.frame` – Ana Oct 30 '18 at 00:31

1 Answers1

1

We can do this with count if the expected output is a summarized one

library(dplyr)
df %>%
   count(category1)

If we need to create a column, use mutate after group_by 'category1'

df %>%
   group_by(category1) %>%
   mutate(n = n())
akrun
  • 874,273
  • 37
  • 540
  • 662