[R] Transform a list of multiple to a data.frame which I want
Jim Lemon
drjimlemon at gmail.com
Mon Feb 2 10:24:58 CET 2015
Hi Yao,
Messy, but this is the closest I can get:
yhlist<-list(K=matrix(0,ncol=2,nrow=0),
GSEGTCSCSSK=matrix(c(6,8),nrow=2,ncol=2),
GFSTTCPAHVDDLTPEQVLDGDVNELMDVVLHHVPEAK=matrix(6,ncol=2,nrow=1),
LVECIGQELIFLLPNK=matrix(6,ncol=2,nrow=1),
NFK=matrix(0,ncol=2,nrow=0),
HR=matrix(0,ncol=2,nrow=0),
AYASLFR=matrix(0,ncol=2,nrow=0))
colnames(yhlist$K)<-c("start","end")
colnames(yhlist$GSEGTCSCSSK)<-c("start","end")
colnames(yhlist$GFSTTCPAHVDDLTPEQVLDGDVNELMDVVLHHVPEAK)<-c("start","end")
colnames(yhlist$LVECIGQELIFLLPNK)<-c("start","end")
colnames(yhlist$NFK)<-c("start","end")
colnames(yhlist$HR)<-c("start","end")
colnames(yhlist$AYASLFR)<-c("start","end")
crunch<-function(x) {
start.1<-ifelse(dim(x)[1],x[1,1],NA)
start.2<-ifelse(dim(x)[1]>1,x[2,1],NA)
return(list(start.1,start.2))
}
crunchlist<-sapply(yhlist,crunch,simplify=TRUE)
ID<-colnames(crunchlist)
colnames(crunchlist)<-NULL
data.frame(ID,
start.1=unlist(crunchlist[1,]),
start.2=unlist(crunchlist[2,]))
Jim
On Sun, Feb 1, 2015 at 11:53 PM, Yao He <heyao at nibs.ac.cn> wrote:
> Dear all:
>
> I have a list like that,which is a standard str_locate_all() function (stringr package) output:
> $K
> start end
> $GSEGTCSCSSK
> start end
> [1,] 6 6
> [2,] 8 8
> $GFSTTCPAHVDDLTPEQVLDGDVNELMDVVLHHVPEAK
> start end
> [1,] 6 6
> $LVECIGQELIFLLPNK
> start end
> [1,] 4 4
> $NFK
> start end
> $HR
> start end
> $AYASLFR
> start end
>
> I want to transform this list like that:
>
> ID start.1 start.2
> K NA NA
> GSEGTCSCSSK 6 8
> GFSTTCPAHVDDLTPEQVLDGDVNELMDVVLHHVPEAK 6 NA
> LVECIGQELIFLLPNK 4 NA
> NFK NA NA
> HR NA NA
> AYASLFR NA NA
>
> I have already tried to use t() , lapply() but I think it is hard to handle the NA value and different rows in every matrix
>
> Thanks in advance
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list