I have a data table which looks like the following:
library(data.table)
Well <- c("A1", "A2", "A3", "A4", "A5")
Episyrphus <- c(0,0,5,3,1)
Eupeodes <- c(2,0,4,1,0)
Syrphus <- c(0,4,0,3,2)
dt <- data.table(Well, Episyrphus, Eupeodes, Syrphus)
dt
Well Episyrphus Eupeodes Syrphus
1: A1 0 2 0
2: A2 0 0 4
3: A3 5 4 0
4: A4 3 1 3
5: A5 1 0 2
What I want to do is change all of the non-zero values in the species columns (Episyrphus, Eupeodes, Syrphus) to 1, so that the table becomes:
Well Episyrphus Eupeodes Syrphus
1: A1 0 1 0
2: A2 0 0 1
3: A3 1 1 0
4: A4 1 1 1
5: A5 1 0 1
However, my problem is that I haven't been able to replace non-zeros with 1 across several columns. The solution I've reached for a single column is:
dt[Episyrphus > 0, Episyrphus := 1L]
But is there any way to use this same code structure, with perhaps a for loop(?), to perform the same function across all species columns?
I feel like the answer is probably obvious, but it's not to me! Any help would be much appreciated.