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 saws there are too many rows.
Many thanks for your help
Would an ifelse be helpful here?
CodePudding user response:
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
CodePudding user response:
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!