Part 1 I have the following data table. I want to make a new column which contains the number of occurrences of each id
where there is any style value, except for NA
. The main problem is that I don't know how to deal with the NA
. Currently, when NA
is present I get a frequency of 1.
id style
1 A
1 A
2 A
2 B
3 NA
4 A
4 C
5 NA
I tried using the following, but it still counts NA
values
dt[, allele_count := .N, by = list(pat_id, style)]
The desired data table would be as follows:
id style count
1 A 2
1 A 2
2 A 2
2 B 2
3 NA 0
4 A 4
4 B 4
4 B 4
4 C 4
5 NA 0
Part2 I would also like to be able to add another column which would have the number of occurrences of each id
with a certain style value.
id style count2
1 A 2
1 A 2
2 A 1
2 B 1
3 NA 0
4 A 1
4 B 2
4 B 2
4 C 1
5 NA 0
Bonus Question: Instead of looking at how many times an id
occurs with a given style
value as in Part2 , How can you calculate the number of different style
values for each id
, as follows.
id style count3
1 A 1
1 A 1
2 A 2
2 B 2
3 NA 0
4 A 3
4 B 3
4 B 3
4 C 3
5 NA 0