I need to count two different columns in a data frame and add the values as two new columns.
In the example below, I want to count x and y
ID l x y
1 s 1 E
1 s 2 NA
1 s 3 E
1 s 4 E
1 s 5 E
2 ss 1 NA
2 ss 2 E
2 ss 3 E
3 m 1 NA
3 m 2 E
3 m 3 NA
3 m 4 NA
3 m 5 E
3 m 6 E
3 m 7 NA
4 mm 1 E
4 mm 2 E
and I need the output to look like the below:
ID l n.x n.y
1 s 5 4
2 ss 3 2
3 m 7 3
4 mm 2 2
How can I use count ()? or other codes?
Thanks
>Solution :
Here is a tidyverse approach. You can group_by your ID column, and count rows that is not NA.
library(tidyverse)
df %>%
group_by(ID, l) %>%
summarize(n.x = sum(!is.na(x)), n.y = sum(!is.na(y)), .groups = "drop")
# A tibble: 4 x 4
ID l n.x n.y
<int> <chr> <int> <int>
1 1 s 5 4
2 2 ss 3 2
3 3 m 7 3
4 4 mm 2 2