Remove specific rows from nested list of matrices

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

Remove specific rows from nested list of matrices

Ek Esawi
Hi All,

I have a list that is made up of nested lists, as shown below. I want
to remove all rows in each sub-list that start with an empty space,
that’s the first entry of a row is blank; for example, on
[[1]][[1]][[1]] Remove row 4,on [[1]][[1]][[3]] remove row 5, on
[[1]][[2]][[1]] remove row 6, etc.. All rows start with 2 digits/ 2
digits. My formula works on individual sublist but not the whole
list.. I know my indexing is wrong, but don’t know how to fix it.


> FF

[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[,1]    [,2]   [,3]    [,4] [,5]
[1,] "30/20"   "AAAAAAAA" “    “   "-89"
[2,] "02/20"   "AAAAAAAA” “    “   "-98"
[3,] "02/20"   “AAAAAAA” “    “   "-84"
[4,] “  “ “  “   “
[[1]][[1]][[2]]
[,1]    [,2]
[1,] "02/23" “AAAAAAAA” : 29" “
[2,] "02/23" “AAAAAAAA” ." “
[3,] "02/23" “AAAAAAAA” " “
[4,] "02/23" “AAAAAAAA” "
[[1]][[1]][[3]]
[,1]    [,2]    [,3] [,4] [,5] [,6] [,7]
[1,] "01/09" “AAAAAAAA"    “   “   “   "53"
[2,] "01/09" “AAAAAAAA” "   “   “   “   "403"
[3,] "01/09" “AAAAAAAA” "   “   “   “   "83"
[4,] "01/09" “AAAAAAAA” "   “   “   “   "783"
[5,] “  “  “AAAAAAAA”  3042742181"   “   “   “   “
[[1]][[2]]
[[1]][[2]][[1]]
[,1]  [,2] [,3] [,4] [,5]
[1,] “    “   “   “   “AAAAAAAA” "
[2,] "Standard Purchases"  “   “   “   "
[3,] "24/90 "AAAAAAAA” “   "243"  "
[4,] "24/90 "AAAAAAAA” "   "143"  "
[5,] "24/91 "AAAAAAAA” " “   "143" “
[6,] “    “   “   “   "792"
[[1]][[2]][[2]]
[,1]    [,2]
[1,] "02/23" “AAAAAAAA”: 31" “
[2,] "02/23" “AAAAAAAA”." “
[3,] "02/23" “AAAAAAAA” " “
[4,] "02/23" “AAAAAAAA”
[5,] "02/23" “AAAAAAAA”
[6,] "02/23" “AAAAAAAA” 20"
[7,] "02/23" “AAAAAAAA”  “
[8,] "02/23" “AAAAAAAA” "33"
[[1]][[3]]
[[1]][[3]][[1]]
[,1]    [,2]
[1,] "02/23" “AAAAAAAA”: 28" “
[2,] "02/23" “AAAAAAAA”." “
[3,] "02/23" “AAAAAAAA” " “
[4,] "02/23" “AAAAAAAA” "
[[1]][[3]][[2]]
[,1]    [,2]    [,3]    [,4] [,5] [,6] [,7]    [,8]    [,9]
[1,] "02/23" “AAAAAAAA” " “   “   "53" "
[2,] "02/24" “AAAAAAAA” " “   “   "
[3,] “  “  “  “   “   “   “  “  "1,241"
[4,] "02/24" "AAAAAAAA”  “   "33”

My Formula,:

G <- lapply(FF, function(x) lapply(x, function (y) lapply(y,
function(z)  z[grepl("^[0-9][0-9]/",z[,1]),])))

The error: Error in z[, 1] : incorrect number of dimensions



Thanks in advance--EK

______________________________________________
[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 specific rows from nested list of matrices

Bert Gunter-2
If you learn to use dput() to provide useful examples in your posts, you
are more likely to receive useful help. It is rather difficult to make much
sense of your messy text, though some brave soul(s) may try to help.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Nov 2, 2018 at 8:00 AM Ek Esawi <[hidden email]> wrote:

> Hi All,
>
> I have a list that is made up of nested lists, as shown below. I want
> to remove all rows in each sub-list that start with an empty space,
> that’s the first entry of a row is blank; for example, on
> [[1]][[1]][[1]] Remove row 4,on [[1]][[1]][[3]] remove row 5, on
> [[1]][[2]][[1]] remove row 6, etc.. All rows start with 2 digits/ 2
> digits. My formula works on individual sublist but not the whole
> list.. I know my indexing is wrong, but don’t know how to fix it.
>
>
> > FF
>
> [[1]]
> [[1]][[1]]
> [[1]][[1]][[1]]
> [,1]    [,2]   [,3]    [,4] [,5]
> [1,] "30/20"   "AAAAAAAA" “    “   "-89"
> [2,] "02/20"   "AAAAAAAA” “    “   "-98"
> [3,] "02/20"   “AAAAAAA” “    “   "-84"
> [4,] “  “ “  “   “
> [[1]][[1]][[2]]
> [,1]    [,2]
> [1,] "02/23" “AAAAAAAA” : 29" “
> [2,] "02/23" “AAAAAAAA” ." “
> [3,] "02/23" “AAAAAAAA” " “
> [4,] "02/23" “AAAAAAAA” "
> [[1]][[1]][[3]]
> [,1]    [,2]    [,3] [,4] [,5] [,6] [,7]
> [1,] "01/09" “AAAAAAAA"    “   “   “   "53"
> [2,] "01/09" “AAAAAAAA” "   “   “   “   "403"
> [3,] "01/09" “AAAAAAAA” "   “   “   “   "83"
> [4,] "01/09" “AAAAAAAA” "   “   “   “   "783"
> [5,] “  “  “AAAAAAAA”  3042742181"   “   “   “   “
> [[1]][[2]]
> [[1]][[2]][[1]]
> [,1]  [,2] [,3] [,4] [,5]
> [1,] “    “   “   “   “AAAAAAAA” "
> [2,] "Standard Purchases"  “   “   “   "
> [3,] "24/90 "AAAAAAAA” “   "243"  "
> [4,] "24/90 "AAAAAAAA” "   "143"  "
> [5,] "24/91 "AAAAAAAA” " “   "143" “
> [6,] “    “   “   “   "792"
> [[1]][[2]][[2]]
> [,1]    [,2]
> [1,] "02/23" “AAAAAAAA”: 31" “
> [2,] "02/23" “AAAAAAAA”." “
> [3,] "02/23" “AAAAAAAA” " “
> [4,] "02/23" “AAAAAAAA”
> [5,] "02/23" “AAAAAAAA”
> [6,] "02/23" “AAAAAAAA” 20"
> [7,] "02/23" “AAAAAAAA”  “
> [8,] "02/23" “AAAAAAAA” "33"
> [[1]][[3]]
> [[1]][[3]][[1]]
> [,1]    [,2]
> [1,] "02/23" “AAAAAAAA”: 28" “
> [2,] "02/23" “AAAAAAAA”." “
> [3,] "02/23" “AAAAAAAA” " “
> [4,] "02/23" “AAAAAAAA” "
> [[1]][[3]][[2]]
> [,1]    [,2]    [,3]    [,4] [,5] [,6] [,7]    [,8]    [,9]
> [1,] "02/23" “AAAAAAAA” " “   “   "53" "
> [2,] "02/24" “AAAAAAAA” " “   “   "
> [3,] “  “  “  “   “   “   “  “  "1,241"
> [4,] "02/24" "AAAAAAAA”  “   "33”
>
> My Formula,:
>
> G <- lapply(FF, function(x) lapply(x, function (y) lapply(y,
> function(z)  z[grepl("^[0-9][0-9]/",z[,1]),])))
>
> The error: Error in z[, 1] : incorrect number of dimensions
>
>
>
> Thanks in advance--EK
>
> ______________________________________________
> [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 specific rows from nested list of matrices

Jeff Newmiller
In reply to this post by Ek Esawi
Can you supply the output of

dput(FF)

?

On November 2, 2018 8:00:08 AM PDT, Ek Esawi <[hidden email]> wrote:

>Hi All,
>
>I have a list that is made up of nested lists, as shown below. I want
>to remove all rows in each sub-list that start with an empty space,
>that’s the first entry of a row is blank; for example, on
>[[1]][[1]][[1]] Remove row 4,on [[1]][[1]][[3]] remove row 5, on
>[[1]][[2]][[1]] remove row 6, etc.. All rows start with 2 digits/ 2
>digits. My formula works on individual sublist but not the whole
>list.. I know my indexing is wrong, but don’t know how to fix it.
>
>
>> FF
>
>[[1]]
>[[1]][[1]]
>[[1]][[1]][[1]]
>[,1]    [,2]   [,3]    [,4] [,5]
>[1,] "30/20"   "AAAAAAAA" “    “   "-89"
>[2,] "02/20"   "AAAAAAAA” “    “   "-98"
>[3,] "02/20"   “AAAAAAA” “    “   "-84"
>[4,] “  “ “  “   “
>[[1]][[1]][[2]]
>[,1]    [,2]
>[1,] "02/23" “AAAAAAAA” : 29" “
>[2,] "02/23" “AAAAAAAA” ." “
>[3,] "02/23" “AAAAAAAA” " “
>[4,] "02/23" “AAAAAAAA” "
>[[1]][[1]][[3]]
>[,1]    [,2]    [,3] [,4] [,5] [,6] [,7]
>[1,] "01/09" “AAAAAAAA"    “   “   “   "53"
>[2,] "01/09" “AAAAAAAA” "   “   “   “   "403"
>[3,] "01/09" “AAAAAAAA” "   “   “   “   "83"
>[4,] "01/09" “AAAAAAAA” "   “   “   “   "783"
>[5,] “  “  “AAAAAAAA”  3042742181"   “   “   “   “
>[[1]][[2]]
>[[1]][[2]][[1]]
>[,1]  [,2] [,3] [,4] [,5]
>[1,] “    “   “   “   “AAAAAAAA” "
>[2,] "Standard Purchases"  “   “   “   "
>[3,] "24/90 "AAAAAAAA” “   "243"  "
>[4,] "24/90 "AAAAAAAA” "   "143"  "
>[5,] "24/91 "AAAAAAAA” " “   "143" “
>[6,] “    “   “   “   "792"
>[[1]][[2]][[2]]
>[,1]    [,2]
>[1,] "02/23" “AAAAAAAA”: 31" “
>[2,] "02/23" “AAAAAAAA”." “
>[3,] "02/23" “AAAAAAAA” " “
>[4,] "02/23" “AAAAAAAA”
>[5,] "02/23" “AAAAAAAA”
>[6,] "02/23" “AAAAAAAA” 20"
>[7,] "02/23" “AAAAAAAA”  “
>[8,] "02/23" “AAAAAAAA” "33"
>[[1]][[3]]
>[[1]][[3]][[1]]
>[,1]    [,2]
>[1,] "02/23" “AAAAAAAA”: 28" “
>[2,] "02/23" “AAAAAAAA”." “
>[3,] "02/23" “AAAAAAAA” " “
>[4,] "02/23" “AAAAAAAA” "
>[[1]][[3]][[2]]
>[,1]    [,2]    [,3]    [,4] [,5] [,6] [,7]    [,8]    [,9]
>[1,] "02/23" “AAAAAAAA” " “   “   "53" "
>[2,] "02/24" “AAAAAAAA” " “   “   "
>[3,] “  “  “  “   “   “   “  “  "1,241"
>[4,] "02/24" "AAAAAAAA”  “   "33”
>
>My Formula,:
>
>G <- lapply(FF, function(x) lapply(x, function (y) lapply(y,
>function(z)  z[grepl("^[0-9][0-9]/",z[,1]),])))
>
>The error: Error in z[, 1] : incorrect number of dimensions
>
>
>
>Thanks in advance--EK
>
>______________________________________________
>[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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[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 specific rows from nested list of matrices

Ek Esawi
Thank you Jeff and Bert. I know i have to use dput add  provide a
reproducible example. The problem is that the output,is huge, has many
nested lists, and the info is private.

Here is the first line of dput(FF) if it helps:
dput(FF)
list(list(list(structure(c("12/30 12/30", "01/02 01/02", "01/02 01/02",

Thanks again--EK
On Fri, Nov 2, 2018 at 11:21 AM Jeff Newmiller <[hidden email]> wrote:

>
> Can you supply the output of
>
> dput(FF)
>
> ?
>
> On November 2, 2018 8:00:08 AM PDT, Ek Esawi <[hidden email]> wrote:
> >Hi All,
> >
> >I have a list that is made up of nested lists, as shown below. I want
> >to remove all rows in each sub-list that start with an empty space,
> >that’s the first entry of a row is blank; for example, on
> >[[1]][[1]][[1]] Remove row 4,on [[1]][[1]][[3]] remove row 5, on
> >[[1]][[2]][[1]] remove row 6, etc.. All rows start with 2 digits/ 2
> >digits. My formula works on individual sublist but not the whole
> >list.. I know my indexing is wrong, but don’t know how to fix it.
> >
> >
> >> FF
> >
> >[[1]]
> >[[1]][[1]]
> >[[1]][[1]][[1]]
> >[,1]    [,2]   [,3]    [,4] [,5]
> >[1,] "30/20"   "AAAAAAAA" “    “   "-89"
> >[2,] "02/20"   "AAAAAAAA” “    “   "-98"
> >[3,] "02/20"   “AAAAAAA” “    “   "-84"
> >[4,] “  “ “  “   “
> >[[1]][[1]][[2]]
> >[,1]    [,2]
> >[1,] "02/23" “AAAAAAAA” : 29" “
> >[2,] "02/23" “AAAAAAAA” ." “
> >[3,] "02/23" “AAAAAAAA” " “
> >[4,] "02/23" “AAAAAAAA” "
> >[[1]][[1]][[3]]
> >[,1]    [,2]    [,3] [,4] [,5] [,6] [,7]
> >[1,] "01/09" “AAAAAAAA"    “   “   “   "53"
> >[2,] "01/09" “AAAAAAAA” "   “   “   “   "403"
> >[3,] "01/09" “AAAAAAAA” "   “   “   “   "83"
> >[4,] "01/09" “AAAAAAAA” "   “   “   “   "783"
> >[5,] “  “  “AAAAAAAA”  3042742181"   “   “   “   “
> >[[1]][[2]]
> >[[1]][[2]][[1]]
> >[,1]  [,2] [,3] [,4] [,5]
> >[1,] “    “   “   “   “AAAAAAAA” "
> >[2,] "Standard Purchases"  “   “   “   "
> >[3,] "24/90 "AAAAAAAA” “   "243"  "
> >[4,] "24/90 "AAAAAAAA” "   "143"  "
> >[5,] "24/91 "AAAAAAAA” " “   "143" “
> >[6,] “    “   “   “   "792"
> >[[1]][[2]][[2]]
> >[,1]    [,2]
> >[1,] "02/23" “AAAAAAAA”: 31" “
> >[2,] "02/23" “AAAAAAAA”." “
> >[3,] "02/23" “AAAAAAAA” " “
> >[4,] "02/23" “AAAAAAAA”
> >[5,] "02/23" “AAAAAAAA”
> >[6,] "02/23" “AAAAAAAA” 20"
> >[7,] "02/23" “AAAAAAAA”  “
> >[8,] "02/23" “AAAAAAAA” "33"
> >[[1]][[3]]
> >[[1]][[3]][[1]]
> >[,1]    [,2]
> >[1,] "02/23" “AAAAAAAA”: 28" “
> >[2,] "02/23" “AAAAAAAA”." “
> >[3,] "02/23" “AAAAAAAA” " “
> >[4,] "02/23" “AAAAAAAA” "
> >[[1]][[3]][[2]]
> >[,1]    [,2]    [,3]    [,4] [,5] [,6] [,7]    [,8]    [,9]
> >[1,] "02/23" “AAAAAAAA” " “   “   "53" "
> >[2,] "02/24" “AAAAAAAA” " “   “   "
> >[3,] “  “  “  “   “   “   “  “  "1,241"
> >[4,] "02/24" "AAAAAAAA”  “   "33”
> >
> >My Formula,:
> >
> >G <- lapply(FF, function(x) lapply(x, function (y) lapply(y,
> >function(z)  z[grepl("^[0-9][0-9]/",z[,1]),])))
> >
> >The error: Error in z[, 1] : incorrect number of dimensions
> >
> >
> >
> >Thanks in advance--EK
> >
> >______________________________________________
> >[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.
>
> --
> Sent from my phone. Please excuse my brevity.

______________________________________________
[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 specific rows from nested list of matrices

Jeff Newmiller
A partial dput is no help at all. A complete dput of part of your data is much more likely to be helpful, but only if you see the same problem in it as you do in the full data set.

As to private data... if you want data handling help in a public forum then you need to create a small set of data that illustrates the problem. If you have to manufacture the data by hand we don't care, but it is up to you to communicate a clear question somehow.

On November 2, 2018 10:50:06 AM PDT, Ek Esawi <[hidden email]> wrote:

>Thank you Jeff and Bert. I know i have to use dput add  provide a
>reproducible example. The problem is that the output,is huge, has many
>nested lists, and the info is private.
>
>Here is the first line of dput(FF) if it helps:
>dput(FF)
>list(list(list(structure(c("12/30 12/30", "01/02 01/02", "01/02 01/02",
>
>Thanks again--EK
>On Fri, Nov 2, 2018 at 11:21 AM Jeff Newmiller
><[hidden email]> wrote:
>>
>> Can you supply the output of
>>
>> dput(FF)
>>
>> ?
>>
>> On November 2, 2018 8:00:08 AM PDT, Ek Esawi <[hidden email]>
>wrote:
>> >Hi All,
>> >
>> >I have a list that is made up of nested lists, as shown below. I
>want
>> >to remove all rows in each sub-list that start with an empty space,
>> >that’s the first entry of a row is blank; for example, on
>> >[[1]][[1]][[1]] Remove row 4,on [[1]][[1]][[3]] remove row 5, on
>> >[[1]][[2]][[1]] remove row 6, etc.. All rows start with 2 digits/ 2
>> >digits. My formula works on individual sublist but not the whole
>> >list.. I know my indexing is wrong, but don’t know how to fix it.
>> >
>> >
>> >> FF
>> >
>> >[[1]]
>> >[[1]][[1]]
>> >[[1]][[1]][[1]]
>> >[,1]    [,2]   [,3]    [,4] [,5]
>> >[1,] "30/20"   "AAAAAAAA" “    “   "-89"
>> >[2,] "02/20"   "AAAAAAAA” “    “   "-98"
>> >[3,] "02/20"   “AAAAAAA” “    “   "-84"
>> >[4,] “  “ “  “   “
>> >[[1]][[1]][[2]]
>> >[,1]    [,2]
>> >[1,] "02/23" “AAAAAAAA” : 29" “
>> >[2,] "02/23" “AAAAAAAA” ." “
>> >[3,] "02/23" “AAAAAAAA” " “
>> >[4,] "02/23" “AAAAAAAA” "
>> >[[1]][[1]][[3]]
>> >[,1]    [,2]    [,3] [,4] [,5] [,6] [,7]
>> >[1,] "01/09" “AAAAAAAA"    “   “   “   "53"
>> >[2,] "01/09" “AAAAAAAA” "   “   “   “   "403"
>> >[3,] "01/09" “AAAAAAAA” "   “   “   “   "83"
>> >[4,] "01/09" “AAAAAAAA” "   “   “   “   "783"
>> >[5,] “  “  “AAAAAAAA”  3042742181"   “   “   “   “
>> >[[1]][[2]]
>> >[[1]][[2]][[1]]
>> >[,1]  [,2] [,3] [,4] [,5]
>> >[1,] “    “   “   “   “AAAAAAAA” "
>> >[2,] "Standard Purchases"  “   “   “   "
>> >[3,] "24/90 "AAAAAAAA” “   "243"  "
>> >[4,] "24/90 "AAAAAAAA” "   "143"  "
>> >[5,] "24/91 "AAAAAAAA” " “   "143" “
>> >[6,] “    “   “   “   "792"
>> >[[1]][[2]][[2]]
>> >[,1]    [,2]
>> >[1,] "02/23" “AAAAAAAA”: 31" “
>> >[2,] "02/23" “AAAAAAAA”." “
>> >[3,] "02/23" “AAAAAAAA” " “
>> >[4,] "02/23" “AAAAAAAA”
>> >[5,] "02/23" “AAAAAAAA”
>> >[6,] "02/23" “AAAAAAAA” 20"
>> >[7,] "02/23" “AAAAAAAA”  “
>> >[8,] "02/23" “AAAAAAAA” "33"
>> >[[1]][[3]]
>> >[[1]][[3]][[1]]
>> >[,1]    [,2]
>> >[1,] "02/23" “AAAAAAAA”: 28" “
>> >[2,] "02/23" “AAAAAAAA”." “
>> >[3,] "02/23" “AAAAAAAA” " “
>> >[4,] "02/23" “AAAAAAAA” "
>> >[[1]][[3]][[2]]
>> >[,1]    [,2]    [,3]    [,4] [,5] [,6] [,7]    [,8]    [,9]
>> >[1,] "02/23" “AAAAAAAA” " “   “   "53" "
>> >[2,] "02/24" “AAAAAAAA” " “   “   "
>> >[3,] “  “  “  “   “   “   “  “  "1,241"
>> >[4,] "02/24" "AAAAAAAA”  “   "33”
>> >
>> >My Formula,:
>> >
>> >G <- lapply(FF, function(x) lapply(x, function (y) lapply(y,
>> >function(z)  z[grepl("^[0-9][0-9]/",z[,1]),])))
>> >
>> >The error: Error in z[, 1] : incorrect number of dimensions
>> >
>> >
>> >
>> >Thanks in advance--EK
>> >
>> >______________________________________________
>> >[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.
>>
>> --
>> Sent from my phone. Please excuse my brevity.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[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 specific rows from nested list of matrices

R help mailing list-2
In reply to this post by Ek Esawi
Since you cannot show the data you have have to learn some R debugging
techniques.

Here is some data that look something like yours and I want to delete rows
of character
matrices whose first entry starts with a space.

  FF <- lapply(1:2,function(i)lapply(1:3, function(j) lapply(1:2,
function(k) if (identical(c(i,j,k), c(2L,3L,1L))) c(" X", "YZ") else
rbind(c("A", "BC", "DE"), c(" P", "QR", "ST")))))
  G <- lapply(FF, function(x) lapply(x, function (y) lapply(y, function(z)
z[grepl("^ ", z[,1]),])))
  #Error in z[, 1] : incorrect number of dimensions

If you don't recognize the problem right away, try setting
  options(error=recover)
which lets you look at objects and evaluate expressions at the time of the
error.   Look at ?recover for details.

  G <- lapply(FF, function(x) lapply(x, function (y) lapply(y, function(z)
z[grepl("^ ", z[,1]),])))
  #Error in z[, 1] : incorrect number of dimensions
  #
  #Enter a frame number, or 0 to exit
  #
  #1: lapply(FF, function(x) lapply(x, function(y) lapply(y, function(z)
z[grepl(
  #2: FUN(X[[i]], ...)
  #3: #1: lapply(x, function(y) lapply(y, function(z) z[grepl("^ ", z[,
1]), ]))
  #4: FUN(X[[i]], ...)
  #5: #1: lapply(y, function(z) z[grepl("^ ", z[, 1]), ])
  #6: FUN(X[[i]], ...)
  #7: #1: grepl("^ ", z[, 1])
  #
  Selection: 6
  #Called from: eval(substitute(browser(skipCalls = skip), list(skip = 7 -
which)),
  #  envir = sys.frame(which))
  #Browse[1]> objects()
  #[1] "z"
  #Browse[1]> str(z)
  # chr [1:2] " X" "YZ"
  #Browse[1]> str(z[,1])
  #Error during wrapup: incorrect number of dimensions
  #Browse[1]>

My guess is that your list includes a mix of matrices and vectors, perhaps
from not using drop=FALSE when you subscripted them earlier.  Add drop=FALSE
to all your calls to "[" when using matrices.






Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, Nov 2, 2018 at 10:50 AM, Ek Esawi <[hidden email]> wrote:

> Thank you Jeff and Bert. I know i have to use dput add  provide a
> reproducible example. The problem is that the output,is huge, has many
> nested lists, and the info is private.
>
> Here is the first line of dput(FF) if it helps:
> dput(FF)
> list(list(list(structure(c("12/30 12/30", "01/02 01/02", "01/02 01/02",
>
> Thanks again--EK
> On Fri, Nov 2, 2018 at 11:21 AM Jeff Newmiller <[hidden email]>
> wrote:
> >
> > Can you supply the output of
> >
> > dput(FF)
> >
> > ?
> >
> > On November 2, 2018 8:00:08 AM PDT, Ek Esawi <[hidden email]> wrote:
> > >Hi All,
> > >
> > >I have a list that is made up of nested lists, as shown below. I want
> > >to remove all rows in each sub-list that start with an empty space,
> > >that’s the first entry of a row is blank; for example, on
> > >[[1]][[1]][[1]] Remove row 4,on [[1]][[1]][[3]] remove row 5, on
> > >[[1]][[2]][[1]] remove row 6, etc.. All rows start with 2 digits/ 2
> > >digits. My formula works on individual sublist but not the whole
> > >list.. I know my indexing is wrong, but don’t know how to fix it.
> > >
> > >
> > >> FF
> > >
> > >[[1]]
> > >[[1]][[1]]
> > >[[1]][[1]][[1]]
> > >[,1]    [,2]   [,3]    [,4] [,5]
> > >[1,] "30/20"   "AAAAAAAA" “    “   "-89"
> > >[2,] "02/20"   "AAAAAAAA” “    “   "-98"
> > >[3,] "02/20"   “AAAAAAA” “    “   "-84"
> > >[4,] “  “ “  “   “
> > >[[1]][[1]][[2]]
> > >[,1]    [,2]
> > >[1,] "02/23" “AAAAAAAA” : 29" “
> > >[2,] "02/23" “AAAAAAAA” ." “
> > >[3,] "02/23" “AAAAAAAA” " “
> > >[4,] "02/23" “AAAAAAAA” "
> > >[[1]][[1]][[3]]
> > >[,1]    [,2]    [,3] [,4] [,5] [,6] [,7]
> > >[1,] "01/09" “AAAAAAAA"    “   “   “   "53"
> > >[2,] "01/09" “AAAAAAAA” "   “   “   “   "403"
> > >[3,] "01/09" “AAAAAAAA” "   “   “   “   "83"
> > >[4,] "01/09" “AAAAAAAA” "   “   “   “   "783"
> > >[5,] “  “  “AAAAAAAA”  3042742181"   “   “   “   “
> > >[[1]][[2]]
> > >[[1]][[2]][[1]]
> > >[,1]  [,2] [,3] [,4] [,5]
> > >[1,] “    “   “   “   “AAAAAAAA” "
> > >[2,] "Standard Purchases"  “   “   “   "
> > >[3,] "24/90 "AAAAAAAA” “   "243"  "
> > >[4,] "24/90 "AAAAAAAA” "   "143"  "
> > >[5,] "24/91 "AAAAAAAA” " “   "143" “
> > >[6,] “    “   “   “   "792"
> > >[[1]][[2]][[2]]
> > >[,1]    [,2]
> > >[1,] "02/23" “AAAAAAAA”: 31" “
> > >[2,] "02/23" “AAAAAAAA”." “
> > >[3,] "02/23" “AAAAAAAA” " “
> > >[4,] "02/23" “AAAAAAAA”
> > >[5,] "02/23" “AAAAAAAA”
> > >[6,] "02/23" “AAAAAAAA” 20"
> > >[7,] "02/23" “AAAAAAAA”  “
> > >[8,] "02/23" “AAAAAAAA” "33"
> > >[[1]][[3]]
> > >[[1]][[3]][[1]]
> > >[,1]    [,2]
> > >[1,] "02/23" “AAAAAAAA”: 28" “
> > >[2,] "02/23" “AAAAAAAA”." “
> > >[3,] "02/23" “AAAAAAAA” " “
> > >[4,] "02/23" “AAAAAAAA” "
> > >[[1]][[3]][[2]]
> > >[,1]    [,2]    [,3]    [,4] [,5] [,6] [,7]    [,8]    [,9]
> > >[1,] "02/23" “AAAAAAAA” " “   “   "53" "
> > >[2,] "02/24" “AAAAAAAA” " “   “   "
> > >[3,] “  “  “  “   “   “   “  “  "1,241"
> > >[4,] "02/24" "AAAAAAAA”  “   "33”
> > >
> > >My Formula,:
> > >
> > >G <- lapply(FF, function(x) lapply(x, function (y) lapply(y,
> > >function(z)  z[grepl("^[0-9][0-9]/",z[,1]),])))
> > >
> > >The error: Error in z[, 1] : incorrect number of dimensions
> > >
> > >
> > >
> > >Thanks in advance--EK
> > >
> > >______________________________________________
> > >[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.
> >
> > --
> > Sent from my phone. Please excuse my brevity.
>
> ______________________________________________
> [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 specific rows from nested list of matrices

R help mailing list-2
In reply to this post by Ek Esawi
It appears that at the bottom of the nesting, so to speak, you have a character matrix.
That is, the contents of the [[1]][[1]][[1]] element is a character matrix that, according to the row and column labels, has 4 rows and 5 columns.
However, the matrix itself, as printed, has, apparently, 4 column in row one, not 5 -- and five quote marks in row 4, so the number of columns is ambiguous (quote marks have to be balanced).

None the less, assuming you really do have character matrices that you're trying to modify, I'd be inclined to take a brute force approach.

I would also use for() loops instead of lapply(), because the code will be easier to follow.

Do you know, or can you assume, the maximum depth of nesting? Let's say it's three.

Here's an outline. I can't test it without an actual object to work on, and it probably has some details wrong. My intent is to present the concept.
(I believe I have the 'next' statements in the right place...)

for (i1 in length(FF)) {
  ## the "1" in "ff1" means first level of nesting, not first element of the list
  ff1 <- FF[[i1]]

  if ( !is.list(ff1) ) {
     ## the current element is not nested list
     {apply the function that removes the appropriate rows}
      ## this 'next' statement is supposed to move us to the 2nd element of FF
     next
  } else {
    ## the current element (of FF) is a list, therefore, have to loop through its elements

  for (i2 in length(ff1)) {
     ff2 <- ff1[[i2]]

     if ( !is.list(ff2) ) {
        ## the current element is not a nested list
        {apply the removal function}
        next
        } else {
        ## the current element is a nested list
       for (i3 in length(ff2) {
      ## if I've kept track correctly, we're now looking at the third level down of nesting,
      ## and if that's the max depth, we don't have to go any further

---- etc, and close all the loops ---


This brute force approach consists of nested for() loops.
The outer loop is for the top level list.
The next nested loop is for the second level lists, within each element of the top level
The next nested loop is for the third level lists, within each second level element
Since not all elements are nested to the same depth, it has to be noticed when a non-list element is reached. That element gets modified and that level is done; move up to the previous level and continue to its next element.
At least, that's an approach that I think can work, but getting all the details correct will take some work.

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 11/2/18, 8:00 AM, "R-help on behalf of Ek Esawi" <[hidden email] on behalf of [hidden email]> wrote:

    Hi All,
   
    I have a list that is made up of nested lists, as shown below. I want
    to remove all rows in each sub-list that start with an empty space,
    that’s the first entry of a row is blank; for example, on
    [[1]][[1]][[1]] Remove row 4,on [[1]][[1]][[3]] remove row 5, on
    [[1]][[2]][[1]] remove row 6, etc.. All rows start with 2 digits/ 2
    digits. My formula works on individual sublist but not the whole
    list.. I know my indexing is wrong, but don’t know how to fix it.
   
   
    > FF
   
    [[1]]
    [[1]][[1]]
    [[1]][[1]][[1]]
    [,1]    [,2]   [,3]    [,4] [,5]
    [1,] "30/20"   "AAAAAAAA" “    “   "-89"
    [2,] "02/20"   "AAAAAAAA” “    “   "-98"
    [3,] "02/20"   “AAAAAAA” “    “   "-84"
    [4,] “  “ “  “   “
    [[1]][[1]][[2]]
    [,1]    [,2]
    [1,] "02/23" “AAAAAAAA” : 29" “
    [2,] "02/23" “AAAAAAAA” ." “
    [3,] "02/23" “AAAAAAAA” " “
    [4,] "02/23" “AAAAAAAA” "
    [[1]][[1]][[3]]
    [,1]    [,2]    [,3] [,4] [,5] [,6] [,7]
    [1,] "01/09" “AAAAAAAA"    “   “   “   "53"
    [2,] "01/09" “AAAAAAAA” "   “   “   “   "403"
    [3,] "01/09" “AAAAAAAA” "   “   “   “   "83"
    [4,] "01/09" “AAAAAAAA” "   “   “   “   "783"
    [5,] “  “  “AAAAAAAA”  3042742181"   “   “   “   “
    [[1]][[2]]
    [[1]][[2]][[1]]
    [,1]  [,2] [,3] [,4] [,5]
    [1,] “    “   “   “   “AAAAAAAA” "
    [2,] "Standard Purchases"  “   “   “   "
    [3,] "24/90 "AAAAAAAA” “   "243"  "
    [4,] "24/90 "AAAAAAAA” "   "143"  "
    [5,] "24/91 "AAAAAAAA” " “   "143" “
    [6,] “    “   “   “   "792"
    [[1]][[2]][[2]]
    [,1]    [,2]
    [1,] "02/23" “AAAAAAAA”: 31" “
    [2,] "02/23" “AAAAAAAA”." “
    [3,] "02/23" “AAAAAAAA” " “
    [4,] "02/23" “AAAAAAAA”
    [5,] "02/23" “AAAAAAAA”
    [6,] "02/23" “AAAAAAAA” 20"
    [7,] "02/23" “AAAAAAAA”  “
    [8,] "02/23" “AAAAAAAA” "33"
    [[1]][[3]]
    [[1]][[3]][[1]]
    [,1]    [,2]
    [1,] "02/23" “AAAAAAAA”: 28" “
    [2,] "02/23" “AAAAAAAA”." “
    [3,] "02/23" “AAAAAAAA” " “
    [4,] "02/23" “AAAAAAAA” "
    [[1]][[3]][[2]]
    [,1]    [,2]    [,3]    [,4] [,5] [,6] [,7]    [,8]    [,9]
    [1,] "02/23" “AAAAAAAA” " “   “   "53" "
    [2,] "02/24" “AAAAAAAA” " “   “   "
    [3,] “  “  “  “   “   “   “  “  "1,241"
    [4,] "02/24" "AAAAAAAA”  “   "33”
   
    My Formula,:
   
    G <- lapply(FF, function(x) lapply(x, function (y) lapply(y,
    function(z)  z[grepl("^[0-9][0-9]/",z[,1]),])))
   
    The error: Error in z[, 1] : incorrect number of dimensions
   
   
   
    Thanks in advance--EK
   
    ______________________________________________
    [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 specific rows from nested list of matrices

Ek Esawi
In reply to this post by Ek Esawi
Thank you all, Bert, Jeff, Bill an Don. I realized I made a silly
mistake in list indexing. Once I saw Bills’ suggestion and was able to
wrap my head around indexing recursive lists, I resolved the problem.
For future readers, here is the answers, even though the question may
not have been clear. I tried Don’s idea and it worked too.

 To filter out rows that start with an empty (i.e. start with numbers,
in this case) string, I used Bill’s suggestion.
G <- lapply(FF, function(x) lapply(x, function (y) lapply(y,
function(z)   z[grepl("^[0-9][0-9]/",z[,1]),])))
S1 <-"\\sÂ.*\\s|^[0-9]x.*|.*[P-p]oints.*|.*\\sto\\s.*"
To remove some unwanted entries, I used this formula.
F <- lapply(G, function(x) lapply(x, function (y) lapply(y,
function(z) gsub(S1,"",z))))

Thanks again--EK
On Fri, Nov 2, 2018 at 11:00 AM Ek Esawi <[hidden email]> wrote:

>
> Hi All,
>
> I have a list that is made up of nested lists, as shown below. I want
> to remove all rows in each sub-list that start with an empty space,
> that’s the first entry of a row is blank; for example, on
> [[1]][[1]][[1]] Remove row 4,on [[1]][[1]][[3]] remove row 5, on
> [[1]][[2]][[1]] remove row 6, etc.. All rows start with 2 digits/ 2
> digits. My formula works on individual sublist but not the whole
> list.. I know my indexing is wrong, but don’t know how to fix it.
>
>
> > FF
>
> [[1]]
> [[1]][[1]]
> [[1]][[1]][[1]]
> [,1]    [,2]   [,3]    [,4] [,5]
> [1,] "30/20"   "AAAAAAAA" “    “   "-89"
> [2,] "02/20"   "AAAAAAAA” “    “   "-98"
> [3,] "02/20"   “AAAAAAA” “    “   "-84"
> [4,] “  “ “  “   “
> [[1]][[1]][[2]]
> [,1]    [,2]
> [1,] "02/23" “AAAAAAAA” : 29" “
> [2,] "02/23" “AAAAAAAA” ." “
> [3,] "02/23" “AAAAAAAA” " “
> [4,] "02/23" “AAAAAAAA” "
> [[1]][[1]][[3]]
> [,1]    [,2]    [,3] [,4] [,5] [,6] [,7]
> [1,] "01/09" “AAAAAAAA"    “   “   “   "53"
> [2,] "01/09" “AAAAAAAA” "   “   “   “   "403"
> [3,] "01/09" “AAAAAAAA” "   “   “   “   "83"
> [4,] "01/09" “AAAAAAAA” "   “   “   “   "783"
> [5,] “  “  “AAAAAAAA”  3042742181"   “   “   “   “
> [[1]][[2]]
> [[1]][[2]][[1]]
> [,1]  [,2] [,3] [,4] [,5]
> [1,] “    “   “   “   “AAAAAAAA” "
> [2,] "Standard Purchases"  “   “   “   "
> [3,] "24/90 "AAAAAAAA” “   "243"  "
> [4,] "24/90 "AAAAAAAA” "   "143"  "
> [5,] "24/91 "AAAAAAAA” " “   "143" “
> [6,] “    “   “   “   "792"
> [[1]][[2]][[2]]
> [,1]    [,2]
> [1,] "02/23" “AAAAAAAA”: 31" “
> [2,] "02/23" “AAAAAAAA”." “
> [3,] "02/23" “AAAAAAAA” " “
> [4,] "02/23" “AAAAAAAA”
> [5,] "02/23" “AAAAAAAA”
> [6,] "02/23" “AAAAAAAA” 20"
> [7,] "02/23" “AAAAAAAA”  “
> [8,] "02/23" “AAAAAAAA” "33"
> [[1]][[3]]
> [[1]][[3]][[1]]
> [,1]    [,2]
> [1,] "02/23" “AAAAAAAA”: 28" “
> [2,] "02/23" “AAAAAAAA”." “
> [3,] "02/23" “AAAAAAAA” " “
> [4,] "02/23" “AAAAAAAA” "
> [[1]][[3]][[2]]
> [,1]    [,2]    [,3]    [,4] [,5] [,6] [,7]    [,8]    [,9]
> [1,] "02/23" “AAAAAAAA” " “   “   "53" "
> [2,] "02/24" “AAAAAAAA” " “   “   "
> [3,] “  “  “  “   “   “   “  “  "1,241"
> [4,] "02/24" "AAAAAAAA”  “   "33”
>
> My Formula,:
>
> G <- lapply(FF, function(x) lapply(x, function (y) lapply(y,
> function(z)  z[grepl("^[0-9][0-9]/",z[,1]),])))
>
> The error: Error in z[, 1] : incorrect number of dimensions
>
>
>
> Thanks in advance--EK

______________________________________________
[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.