0

I have a very large data.table with the structure below:

variant ID1 ID2 ID3 ID4 .... ID80000
123     0    1   2   1         0
321     1    2   1   1         1
543     1    1   2   1         1
6542    1    0   0   1         0  
243     1    0   2   1         1
654     0    1   1   2         1 
342     1    2   1   2         1

I would like to add a row with a 0 or 1 dependant on whether the ID in a string is present in the above table.

Say I have the a list: a <- c("ID42", "ID4")

The desired output would be:

variant ID1 ID2 ID3 ID4 .... ID80000
123     0    1   2   1         0
321     1    2   1   1         1
543     1    1   2   1         1
6542    1    0   0   1         0  
243     1    0   2   1         1
654     0    1   1   2         1 
342     1    2   1   2         1
present 0    1   0   1         0

A bit like this question but in rows: Create new conditional column if string contains elements from list

I have tried:

df$present <- colnames(df) %in% id_list 

But I get an error as it says there are too many rows.

Many thanks for your help

Would an ifelse be helpful here?

tacrolimus
  • 500
  • 2
  • 12

2 Answers2

1

I created a similar structure as your df, and append a row to the dataframe.

df <- data.frame(matrix(ncol=10, nrow=0)) 
colnames(df) <- c("variant", rep(paste0("ID",2:ncol(df))))
a <- c("ID42", "ID4")
df[nrow(df) + 1,] <- c("present", ifelse(colnames(df) %in% a, 1, 0)[-1])
df

variant ID2 ID3 ID4 ID5 ID6 ID7 ID8 ID9 ID10
present   0   0   1   0   0   0   0   0    0
tavdp
  • 421
  • 2
  • 6
  • 11
0

Thanks to @sashahafner above:

I got df['present',] <-colnames(df) %in% id_list  

to work, not sure it's the best solution but hey ho!

tacrolimus
  • 500
  • 2
  • 12
  • 1
    Hi @tacrolimus. Don't know if this is important but your solution leads to having "present" as `rowname`. But in the desired result indicated in your question "present" is in the `variant` column... – lovalery Nov 17 '21 at 15:06
  • thanks for taking the time to have a look. Yes that's what I was hoping for! – tacrolimus Nov 17 '21 at 15:14
  • 1
    O.K. if you got the result you were hoping for, then everything is fine. Cheers. – lovalery Nov 17 '21 at 15:17