Reputation: 1080
I have a data.frame with 16 columns. Here's one example row.
> data[16,]
V1 V2 V3 V4
16 comp27182_c0_seq4 ENSP00000442096 ENSG00000011143 ENSFCAP00000011376
V5 V6 V7 V8
16 ENSFCAG00000012261 comp48601_c0_seq1 comp19130_c0_seq3 comp22796_c2_seq3
V9 V10 V11 V12
16 comp146901_c0_seq1 comp157916_c0_seq1 comp158124_c0_seq1
V13 V14 V15 V16
16 comp229797_c0_seq1 comp61875_c0_seq2
I'm only interested in columns 1 and 6-16. The first column contains the name I would like to use as a column name in the matrix, 6 to 16 may contain either a string or '' (nothing). I would like to transform this data.frame into a matrix showing 1 or 0, reflecting the content in columns 6-16.
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
comp27182_c0_seq4 1 1 1 1 0 1 1 1 1 0 0
I've trying to use mask without success. I'm sure there's a very easy option out there.
Thanks for any help.
Upvotes: 1
Views: 1284
Reputation: 1080
I slightly modified your code to my exact needs. Now the first column is naming the rows.
a<-do.call(cbind, lapply(c(6:16),
function(x) as.numeric(nchar(as.character(data[,x])) > 0)))
rownames(a)<-data[,1]
It works great, thanks!
Upvotes: 0
Reputation: 9830
Try this:
do.call(cbind, lapply(c(1,6:16),
function(x) as.numeric(nchar(as.character(data[,x])) > 0)))
Upvotes: 4