[R] remove text from nested list
William Dunlap
wdun|@p @end|ng |rom t|bco@com
Fri Oct 26 05:10:44 CEST 2018
If your matrices are at various depths in the list, try rapply(). E.g.,
> L <- list( A = list( a1 = matrix(c("AAA","AB", "AAB","AC"),2,2),
a2=c("AAx")), list(B = c("AAb1AAA","AAb2")))
> str(L)
List of 2
$ A:List of 2
..$ a1: chr [1:2, 1:2] "AAA" "AB" "AAB" "AC"
..$ a2: chr "AAx"
$ :List of 1
..$ B: chr [1:2] "AAb1AAA" "AAb2"
> str(rapply(L, function(x)gsub("A+", "-", x), how="replace"))
List of 2
$ A:List of 2
..$ a1: chr [1:2, 1:2] "-" "-B" "-B" "-C"
..$ a2: chr "-x"
$ :List of 1
..$ B: chr [1:2] "-b1-" "-b2"
> # only apply f to matrices in the list:
> str(rapply(L, function(x)gsub("A+", "-", x), classes="matrix",
how="replace"))
List of 2
$ A:List of 2
..$ a1: chr [1:2, 1:2] "-" "-B" "-B" "-C"
..$ a2: chr "AAx"
$ :List of 1
..$ B: chr [1:2] "AAb1AAA" "AAb2"
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Thu, Oct 25, 2018 at 6:04 PM, Ek Esawi <esawiek using gmail.com> wrote:
> Hi All—
>
> I have a list that contains multiple sub-lists and each sub-list
> contains multiple sub(sub-lists), each of the sub(sub-lists) is made
> up of matrices of text. I want to replace some of the text in some
> parts in the matrices on the list. I tried gsub and stringr,
> str_remove, but nothing seems to work
>
> I tried:
>
> lapply(mylist, function(x) lapply(x, function(y)
> gsub("[0-9][0-9]/[0-9[0-9].*com","",y)))
> lapply(mylist, function(x) str_remove(x,"[0-9][0-9]/[0-9[0-9].*com"))
>
> Any help is greatly apprercaited.
>
>
>
> mylist—this is just an example
>
> [[1]]
> [[1]][[1]]
> [[1]][[1]][[1]]
> [,1] [,2] [,3] [,4] [,5]
> [1,] "12/30 12/30" "ABABABABABAB" "8.00"
> [2,] "01/02 01/02" "AAAAAAAAAAAA”. “99"
> [3,] "01/02 01/02" "CACACACACACC” "55.97"
>
> [[1]][[1]][[2]]
> [,1] [,2]
> [1,] "12/30 12/30" "DDDDDDDDDDDDDDD” “29"
> [2,] "12/30 12/30" :GGGGGGGGGGGGGGG” “333”
>
> [[1]][[2]]
> [[1]][[2]][[1]]
> [,1] [,2] [,3] [,4] [,5]
> [1,] "01/02 01/02" "ThankYou" “23”
> [2,] "01/02 01/02" "Standard data" "251"
>
> ______________________________________________
> R-help using 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