I have a table with 2 columns, "name"
and "grade"
. In "name"
column I store data that can be replicated couple of times. To imagine the problem, let's create a simple short table like the one below:
list <- data.frame(c("Natalia", "Alex", "Adam", "Natalia", "Natalia", "Alex", "Natalia", "Adam"), c(5, 6, 5, 4, 5, 4, 3, 4))
colnames(list) <- c("name", "grade")
I'd like to get a dataframe
with two columns - a list of unique data from column "name"
in first one and with a sum of grades for each name in second.
The first column I created like that:
n_occur <- data.frame(table(list$name))
and it works - I have a column of unique names from previous table.
Unfortunately I have no idea how to count grades for each name. It's more or less sth like pseudocode below, but I don't know r syntax well, so it's a bit hard for me.
sum(list$grades) where (list$names == n_occur$X1)
I think that I should combine filter with select somehow, but I didn't manage to do that.