Reputation: 149
Hi guys so I am working with a large data frame that records employee time card data for specific months. I want to print out the entire row of employees who record a MAXIMUM of three zeros in three different months. I believe I need to use the apply function: apply(Employee, 1, ...) where Employee is the name of the data frame, 1 allows it to iterate over each row, but I am unsure how to select for only those that have three or fewer zeros in its row. I appreciate the help!
Upvotes: 1
Views: 3986
Reputation: 99331
You could use rowSums()
. Here's an example with some fake data.
> d <- data.frame(X1 = c(0, 1, 0, 0), X2 = c(0, 2, 2, 0),
X3 = c(0, 2, 0, 0), X4 = c(3, 0, 0, 0))
> d
# X1 X2 X3 X4
# 1 0 0 0 3
# 2 1 2 2 0
# 3 0 2 0 0
# 4 0 0 0 0
The we can subset for those rows where the logical value of x == 0
sums to 3 or less:
> d[rowSums(d == 0, na.rm = TRUE) <= 3, ]
# X1 X2 X3 X4
# 1 0 0 0 3
# 2 1 2 2 0
# 3 0 2 0 0
Upvotes: 3
Reputation: 183
How about something just using which
and rowSums
instead of apply
?
Employee[which(rowSums(Employee==0)<=3),]
Upvotes: 1