Yao He
2015-Feb-01 12:53 UTC
[R] Transform a list of multiple to a data.frame which I want
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
Jim Lemon
2015-Feb-02 09:24 UTC
[R] Transform a list of multiple to a data.frame which I want
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.
William Dunlap
2015-Feb-02 15:53 UTC
[R] Transform a list of multiple to a data.frame which I want
Does the following work for you. The only trick is working around the fact that matrix subscripting does not allow out-of-bounds subscripts but vector subscripting does. We do the subscripting in two steps, relying on the drop=TRUE default in the matrix subscripting operator that converts the single- column matrix output to a vector.> txt <- c("abc", "abcade", "xyz", "abacad") > sTxt <- str_locate_all(txt, "a") > lTxt <- lapply(sTxt, function(st)structure(st[,1][1:2],names=c("start.1", "start.2")))> names(lTxt) <- txt > str(lTxt)List of 4 $ abc : Named int [1:2] 1 NA ..- attr(*, "names")= chr [1:2] "start.1" "start.2" $ abcade: Named int [1:2] 1 4 ..- attr(*, "names")= chr [1:2] "start.1" "start.2" $ xyz : Named num [1:2] NA NA ..- attr(*, "names")= chr [1:2] "start.1" "start.2" $ abacad: Named int [1:2] 1 3 ..- attr(*, "names")= chr [1:2] "start.1" "start.2" Bill Dunlap TIBCO Software wdunlap tibco.com On Sun, Feb 1, 2015 at 4:53 AM, 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. >[[alternative HTML version deleted]]