[R] Transform a list of multiple to a data.frame which I want
William Dunlap
wdunlap at tibco.com
Mon Feb 2 16:53:08 CET 2015
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]]
More information about the R-help
mailing list