What I have:

I have a dataframe with approx. 20,000 data records.

In the first column are the names of employees ... in the second the value “participated” or “did not participate”.

Each row represents a specific training course.

What I want:

I want to count how many times an employee has attended an event. The result should then be output in a new dataframe.

Condition:

Count all seminars of the employee “Smith” in the first column if the second column contains “participated”.

---

Another column (“Duration”) shows the number of minutes of the course.

In a further run, I would now like to determine the total duration of the course times.

Condition:

Add all the values from the “Duration” column for each employee if the second column contains “participated”.

---

I haven't found anything suitable online.

I also don't want to separate the table beforehand by filtering out the rows in which the second column says “did not participate”.

3 Replies 3

IMHO, this is not a best practices question. It's a simple "How to?". It has been answered many, many times before on this site. See, for example, this. You maximise your chance of getting a useful answer if you provide a minimal reproducible example. This post may help.

"I haven't found anything suitable online." If you search on Stack Overflow for [R] count, a bunch of the top results will probably be helpful to you.

After loading library(dplyr), I might use your_dataframe |> count(Employee_name, wt = participation_column == "participated"), but update that with whatever your dataframe and it's columns are called. Or your_dataframe |> summarize(times_participated = sum(participation_column == "participated"), total_duration = sum(Duration), .by = Employee_name)

Here's the R-FAQ on summarizing multiple columns by group. As Jon Spring says, the summary functions you want to apply here are sum(participation_column == "participated") to get the pariticipation count and sum(Duration[participation_column == "participated"]) to get the total duration.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.