Turning all table elements into column IDs then new table 0 or 1 if the patient (row ID) had that element

I'm pretty sure mutate(row=as.numeric(ID) is not working it seems to not change id1 id2 and id3 to 1 2 and 3 it seems to turn them all to NA which is NOT what we want. Warning, I cannot expect my real patient data to have patient IDs that contain the row numbers like that.

Warning message:
Problem with mutate() column row.
:information_source: row = as.numeric(ID).
:information_source: NAs introduced by coercion

Not even your original code works I am suspicious about all the row being NA

plong_0 <- patients %>% pivot_longer(-ID) %>%
filter(value!="") %>%
mutate(row=as.numeric(ID),
colid=value %>%
forcats::as_factor() %>%
as.numeric)

plong_1 <- plong_0 %>% select(ID,row,colid) %>% collect()

diseases <- distinct(plong_0,value) %>%
mutate(colid=value %>%
forcats::as_factor() %>%
as.numeric ) %>%
collect()

result <- sparseMatrix(i=plong_1$row,
j=plong_1$colid,
x = rep(1,nrow(plong_1)))

Error in sparseMatrix(i = plong_1$row, j = plong_1$colid, x = rep(1, nrow(plong_1))) :
NA's in (i,j) are not allowed

Your new code still doesn't fix lazy_dt I a, using R402 and I still get errors about mutate do we really have to mutate what if we don't?:

librarian::shelf(tidyverse / dtplyr)
library(dplyr)
library(tidyr)
library(forcats)
library(Matrix)
library(fst)
library(pryr)

library(data.table)

patients = data.table(
ID = c("id1", "id2", "id3"),
f.41270.1 = c("184.11", "987", ""),
f.41270.2 = c("151.11", "", ""),
f.41270.3 = c("", "184.11", "")
)

plong_0 <- patients %>% lazy_dt %>%
mutate_all(as.factor) %>%
pivot_longer(-ID) %>%
filter(value!="") %>%
mutate(row=as.numeric(ID),
colid=value %>%
forcats::as_factor() %>%
as.numeric)

Error in UseMethod("pivot_longer") :
no applicable method for 'pivot_longer' applied to an object of class "c('dtplyr_step_mutate', 'dtplyr_step')"

plong_0 <- patients %>%
mutate_all(as.factor) %>%
pivot_longer(-ID) %>%
filter(value!="") %>%
mutate(row=as.numeric(ID),
colid=value %>%
forcats::as_factor() %>%
as.numeric)

Error in UseMethod("pivot_longer") :
no applicable method for 'pivot_longer' applied to an object of class "c('dtplyr_step_mutate', 'dtplyr_step')"

My current issue is

Error in asMethod(object) :
Cholmod error 'problem too large' at file ../Core/cholmod_dense.c, line 102
Calls: write_tsv ... as.data.frame -> as.matrix -> as.matrix -> as -> asMethod

Line 102 is:

99 plong_0 <- patients %>% pivot_longer(-ID) %>%
100 filter(value!="") %>%
101 mutate(row=as.numeric(ID),
102 colid=value %>%
103 forcats::as_factor() %>%
104 as.numeric)