In R data.table I have this table:
Company <- c ("A", "A" , "A", "A", "B" , "B", "B", "B")
TopManger <- c(1, 1, 1, 0, 1, 1, 0, 0)
Salary <- c(300, 300, 300, NA, 250,250, NA, 100)
tbl = data.table(company, TopManger, Salary)
that looks like:
Company TopManger Salary
1: A 1 300
2: A 1 300
3: A 1 300
4: A 0 NA
5: B 1 250
6: B 1 250
7: B 0 NA
8: B 0 100
I perform this query:
tbl[, HighPayedComp := as.numeric(Salary[TopManger == 1] > 200), by=Company]
And I expect to get this:
Company TopManger Salary HighPayedComp
1: A 1 300 1
2: A 1 300 1
3: A 1 300 1
4: A 0 NA 1
5: B 1 250 1
6: B 1 250 1
7: B 0 NA 1
8: B 0 100 1
In this minimal example it works but in my big data.table in some arbitrary but fixed situations coerces NA
where logically it shouldn't:
For example in line 5 I have NA
for HighPayedComp
.