remove text from nested list

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

remove text from nested list

Ek Esawi
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"

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: remove text from nested list

plangfelder
You should be more specific about what you want to replace and with
what. The pattern you use, namely "[0-9][0-9]/[0-9[0-9].*com", does
not (AFAICS) match any of the strings in your data, so don't be
surprised that your commands do not change anything.

If you have a correct pattern and replacement and all lists have depth
3, using something like

lapply(mylist, lapply, lapply, function(y) gsub(pattern, replacement, y))

should work. If your list has a variable depth, I would use a
recursive function, something like

recursiveGSub = function(x, pattern, replacement)
{
  if (is.atomic(x)) gsub(pattern, replacement, x) else lapply(x,
recursiveGSub, pattern, replacement)
}

Example:

lst = list("a001", list("b001", list("c001", "d001")))

lst
recursiveGSub(lst, "00", "")


HTH,

Peter
On Thu, Oct 25, 2018 at 6:04 PM Ek Esawi <[hidden email]> 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"
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: remove text from nested list

Bert Gunter-2
In reply to this post by Ek Esawi
1. Please learn how to use dput() to provide examples to responders.
There's not much we can do with a text printout (at least without some work
that I don't care to do).

2. Do you know what mylist[[c(1,2,1)]] means? If not, read ?(Extract) and
note in particular:
"[[ can be applied recursively to lists, so that if the single index i is a
vector of length p, alist[[i]] is equivalent to alist[[i1]]...[[ip]] providing
all but the final indexing results in a list."

As your intent is unclear -- no reproducible example showing the desired
result -- I would suggest just using list indexing to access the matrices
you wish to change. But maybe this does not satisfy your vague request.

Also, something seems screwy in the example you showed: For example, the
[[1]][[2]][[1]] component indicates a 2 x 5 matrix, but I see only 3
columns of text. Am I missing something?

Cheers,
Bert



On Thu, Oct 25, 2018 at 6:04 PM Ek Esawi <[hidden email]> 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"
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: remove text from nested list

R help mailing list-2
In reply to this post by Ek Esawi
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 <[hidden email]> 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"
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: remove text from nested list

Ek Esawi
In reply to this post by Bert Gunter-2
Thank you Bert and Peter. My apology for posting poor code. I cannot
create a reproducible example of my data, but i hope the list indices
as shown below helps you understand my question.. My regex pattern on
my previous post works correctly because i tested it on a few sublists
and it worked, but did not work for the all lists.
I tend to think a set of lapply and one apply function will work, but
not sure how to do that.

i tried, but obviously i don't understand nested apply functions.

> lapply(mylsit, function(x) gsub(pattern,"",x))
> lapply(mylist, function(x) lapply (x, function(y)  gsub(mypattern,"",y)))
> lappqaly(mylist, function(x) lapply (x, function(y) apply(y,2,gsub(mypattern,"",y))))


[[1]]

[[1]][[1]]
[[1]][[1]][[1]]
[[1]][[1]][[2]]
[[1]][[1]][[3]]

[[1]][[2]]
[[1]][[2]][[1]]
[[1]][[2]][[2]]

[[1]][[3]]
[[1]][[3]][[1]]
[[1]][[3]][[2]]

[[1]][[4]]
[[1]][[4]][[1]]
[[1]][[4]][[2]]
[[1]][[4]][[3]]
[[1]][[4]][[4]]
[[1]][[4]][[5]]

[[1]][[5]]
[[1]][[5]][[1]]
[[1]][[5]][[2]]
[[1]][[5]][[3]]
[[1]][[5]][[4]]

[[1]][[6]]
[[1]][[6]][[1]]
[[1]][[6]][[2]]
[[1]][[6]][[3]]

[[1]][[7]]
[[1]][[7]][[1]]
[[1]][[7]][[2]]
[[1]][[7]][[3]]

[[1]][[8]]
[[1]][[8]][[1]]
[[1]][[8]][[3]]
On Thu, Oct 25, 2018 at 9:34 PM Bert Gunter <[hidden email]> wrote:

>
> 1. Please learn how to use dput() to provide examples to responders. There's not much we can do with a text printout (at least without some work that I don't care to do).
>
> 2. Do you know what mylist[[c(1,2,1)]] means? If not, read ?(Extract) and note in particular:
> "[[ can be applied recursively to lists, so that if the single index i is a vector of length p, alist[[i]] is equivalent to alist[[i1]]...[[ip]] providing all but the final indexing results in a list."
>
> As your intent is unclear -- no reproducible example showing the desired result -- I would suggest just using list indexing to access the matrices you wish to change. But maybe this does not satisfy your vague request.
>
> Also, something seems screwy in the example you showed: For example, the [[1]][[2]][[1]] component indicates a 2 x 5 matrix, but I see only 3 columns of text. Am I missing something?
>
> Cheers,
> Bert
>
>
>
> On Thu, Oct 25, 2018 at 6:04 PM Ek Esawi <[hidden email]> 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"
>>
>> ______________________________________________
>> [hidden email] 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.

______________________________________________
[hidden email] 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.