Re: Thanks for help

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Thanks for help

Jim Lemon-4
Okay, if I understand this, you want to remove all rows that have, for
example, a 1 in any of ten columns:

a<-matrix(sample(1:20,350,TRUE),ncol=10)
# check it out
a
# first do it with a loop
b<-a
for(i in 1:ncol(b)) b<-b[b[,i]!=1,]
b
# now get tricky and do it in one operation
no1s<-apply(a,1,function(x) any(1 %in% x))
no1s
[1]  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE
FALSE
[25]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE
c<-a[!no1s,]
c

Jim

On Fri, Mar 26, 2021 at 8:17 AM Goyani Zankrut <[hidden email]> wrote:

> Example purpose I was created 21x2 matrix.
> My actual matrix is 35x10 matrix. So i have to ran same code 10 times for
> each columns.
>
> On Fri, Mar 26, 2021, 2:44 AM Goyani Zankrut <[hidden email]> wrote:
>
>> I understood that.
>> c<-a[a[,1] != 1,] this will work for first column only.
>>
>> If I'm trying same by this
>> c<-a[a[,1] !=2,]
>> c<-a[a[,2] !=2,]
>> Two times
>> So i was tried for loop but I'm stucked.
>>
>> On Fri, Mar 26, 2021, 2:37 AM Jim Lemon <[hidden email]> wrote:
>>
>>> HI Goyani,
>>> What is happening is that rows and columns are mixed up. You have
>>> specified the sequence as 1:nrow(a)
>>>  and then applied it to the columns a[,i]. The index error occurs
>>> because you have 21 rows but only two columns. That's the bad news. The
>>> good news is that you don't have to use a loop at all:
>>>
>>> c<-a[a[,1] != 1,]
>>>
>>> To see why, look at the expression inside the outer brackets:
>>>
>>> a[,1] != 1
>>>
>>> It produces a logical vector that can be used to index the matrix as you
>>> wish.
>>>
>>> Jim
>>>
>>> On Fri, Mar 26, 2021 at 1:57 AM Goyani Zankrut <[hidden email]>
>>> wrote:
>>>
>>>> Sorry, I'm again disturbing you.
>>>> I have one problem which I'm trying to solve but getting no results.
>>>> Problem statement:
>>>> I created a combination matrix (dimensions 21 * 2). I want to remove
>>>> some entire rows which start with "1". I was tried lapply, apply, and
>>>> for loop for this but I'm still failing. I don't know how to do that.
>>>> I'm sharing the code and image for your reference in this email.
>>>> matrix row remove
>>>>
>>>>                   a<- t(combn(7, 2))
>>>> for(i in 1:nrow(a)) {
>>>>   c<- c(a[a[,i]!=1])
>>>>   print(c)
>>>> }
>>>> # getting this result
>>>> [1] 2 2 2 2 2 3 3 3 3 4 4 4 5 5 6 3 4 5 6 7 4 5 6 7 5 6 7 6 7 7
>>>>  [1] 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 4 4 4 5 5 6 2 3 4 5 6 7 3 4 5 6 7 4 5 6 7 5 6 7
>>>> [40] 6 7 7
>>>> Error in a[, i] : subscript out of bounds
>>>>
>>>>                 [image: Mixmax]
>>>> <https://www.mixmax.com/?ref=Code%20snippet&userId=605ca35fdea6c2667fcbd315> Not
>>>> using Mixmax yet?
>>>> <https://www.mixmax.com/?ref=Code%20snippet&userId=605ca35fdea6c2667fcbd315>
>>>> I want the remaining matrix after removing those rows which starts with
>>>> 1.
>>>> *"Healthy soil, Healthy life."*
>>>> *"A war based on Satyagraha is always of two kinds. One is the war we
>>>> wage against injustice, and the other we fight our won weaknesses."* - *Sardar
>>>> Patel*
>>>> *"You have to dream before your dreams can come true."* - *A. P. J.* *Abdul
>>>> Kalam*
>>>> *"Think before you print and save a tree."*
>>>>
>>>> *ZANKRUT GOYANI*
>>>> *B.Sc. (Hons.) Agriculture*
>>>>
>>>>
>>>>
>>>> On Tue, Mar 23, 2021 2:48 AM, Jim Lemon [hidden email] wrote:
>>>>
>>>>> Hi Goyani,
>>>>>
>>>>> I learned something, too, as you may have noticed.
>>>>>
>>>>>
>>>>> Dr Jim Lemon
>>>>>
>>>>> Gladesville, NSW
>>>>>
>>>>> AUSTRALIA
>>>>>
>>>>>
>>>>> I no longer have a university affiliation.
>>>>>
>>>>>
>>>>> Jim
>>>>>
>>>>>
>>>>> On Tue, Mar 23, 2021 at 3:43 AM Goyani Zankrut <[hidden email]>
>>>>> wrote:
>>>>>
>>>>> >
>>>>>
>>>>> > Greetings of the day,
>>>>>
>>>>> > Sir, can you share your details so I can write them in my thesis
>>>>> work. I want to write everyone's name and their designation & institute
>>>>> name. If you are not belonging to any institute, I mentioned your origin
>>>>> (City, state, Country).
>>>>>
>>>>> > I hope you will send these details.
>>>>>
>>>>> > Again thanks for the help.
>>>>>
>>>>> > "Healthy soil, Healthy life."
>>>>>
>>>>> > "A war based on Satyagraha is always of two kinds. One is the war we
>>>>> wage against injustice, and the other we fight our won weaknesses." -
>>>>> Sardar Patel
>>>>>
>>>>> > "You have to dream before your dreams can come true." - A. P. J.
>>>>> Abdul Kalam
>>>>>
>>>>> > "Think before you print and save a tree."
>>>>>
>>>>> >
>>>>>
>>>>> > ZANKRUT GOYANI
>>>>>
>>>>> > B.Sc. (Hons.) Agriculture
>>>>>
>>>>>
>>>>>
>>>>
>>>> *"Healthy soil, Healthy life."*
>>>> *"A war based on Satyagraha is always of two kinds. One is the war we
>>>> wage against injustice, and the other we fight our won weaknesses."* - *Sardar
>>>> Patel*
>>>> *"You have to dream before your dreams can come true."* - *A. P. J.* *Abdul
>>>> Kalam*
>>>> *"Think before you print and save a tree."*
>>>>
>>>> *ZANKRUT GOYANI*
>>>> *B.Sc. (Hons.) Agriculture*
>>>>
>>>

        [[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: Thanks for help

Bert Gunter-2
apply() is also a (disguised) loop, though.

I think you will find that indexing via rowSums is a lot faster:

## The example
set.seed(111) ## for reproducibility
a<-matrix(sample(1:20,350,TRUE),ncol=10) ## 35 rows

## A one-liner
a[rowSums(a != 1) == 10, ]  ## 20 rows


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 Thu, Mar 25, 2021 at 2:56 PM Jim Lemon <[hidden email]> wrote:

> Okay, if I understand this, you want to remove all rows that have, for
> example, a 1 in any of ten columns:
>
> a<-matrix(sample(1:20,350,TRUE),ncol=10)
> # check it out
> a
> # first do it with a loop
> b<-a
> for(i in 1:ncol(b)) b<-b[b[,i]!=1,]
> b
> # now get tricky and do it in one operation
> no1s<-apply(a,1,function(x) any(1 %in% x))
> no1s
> [1]  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
> [13] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE
> FALSE
> [25]  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE
> c<-a[!no1s,]
> c
>
> Jim
>
> On Fri, Mar 26, 2021 at 8:17 AM Goyani Zankrut <[hidden email]>
> wrote:
>
> > Example purpose I was created 21x2 matrix.
> > My actual matrix is 35x10 matrix. So i have to ran same code 10 times for
> > each columns.
> >
> > On Fri, Mar 26, 2021, 2:44 AM Goyani Zankrut <[hidden email]>
> wrote:
> >
> >> I understood that.
> >> c<-a[a[,1] != 1,] this will work for first column only.
> >>
> >> If I'm trying same by this
> >> c<-a[a[,1] !=2,]
> >> c<-a[a[,2] !=2,]
> >> Two times
> >> So i was tried for loop but I'm stucked.
> >>
> >> On Fri, Mar 26, 2021, 2:37 AM Jim Lemon <[hidden email]> wrote:
> >>
> >>> HI Goyani,
> >>> What is happening is that rows and columns are mixed up. You have
> >>> specified the sequence as 1:nrow(a)
> >>>  and then applied it to the columns a[,i]. The index error occurs
> >>> because you have 21 rows but only two columns. That's the bad news. The
> >>> good news is that you don't have to use a loop at all:
> >>>
> >>> c<-a[a[,1] != 1,]
> >>>
> >>> To see why, look at the expression inside the outer brackets:
> >>>
> >>> a[,1] != 1
> >>>
> >>> It produces a logical vector that can be used to index the matrix as
> you
> >>> wish.
> >>>
> >>> Jim
> >>>
> >>> On Fri, Mar 26, 2021 at 1:57 AM Goyani Zankrut <[hidden email]>
> >>> wrote:
> >>>
> >>>> Sorry, I'm again disturbing you.
> >>>> I have one problem which I'm trying to solve but getting no results.
> >>>> Problem statement:
> >>>> I created a combination matrix (dimensions 21 * 2). I want to remove
> >>>> some entire rows which start with "1". I was tried lapply, apply, and
> >>>> for loop for this but I'm still failing. I don't know how to do that.
> >>>> I'm sharing the code and image for your reference in this email.
> >>>> matrix row remove
> >>>>
> >>>>                   a<- t(combn(7, 2))
> >>>> for(i in 1:nrow(a)) {
> >>>>   c<- c(a[a[,i]!=1])
> >>>>   print(c)
> >>>> }
> >>>> # getting this result
> >>>> [1] 2 2 2 2 2 3 3 3 3 4 4 4 5 5 6 3 4 5 6 7 4 5 6 7 5 6 7 6 7 7
> >>>>  [1] 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 4 4 4 5 5 6 2 3 4 5 6 7 3 4 5 6 7
> 4 5 6 7 5 6 7
> >>>> [40] 6 7 7
> >>>> Error in a[, i] : subscript out of bounds
> >>>>
> >>>>                 [image: Mixmax]
> >>>> <
> https://www.mixmax.com/?ref=Code%20snippet&userId=605ca35fdea6c2667fcbd315>
> Not
> >>>> using Mixmax yet?
> >>>> <
> https://www.mixmax.com/?ref=Code%20snippet&userId=605ca35fdea6c2667fcbd315
> >
> >>>> I want the remaining matrix after removing those rows which starts
> with
> >>>> 1.
> >>>> *"Healthy soil, Healthy life."*
> >>>> *"A war based on Satyagraha is always of two kinds. One is the war we
> >>>> wage against injustice, and the other we fight our won weaknesses."*
> - *Sardar
> >>>> Patel*
> >>>> *"You have to dream before your dreams can come true."* - *A. P. J.*
> *Abdul
> >>>> Kalam*
> >>>> *"Think before you print and save a tree."*
> >>>>
> >>>> *ZANKRUT GOYANI*
> >>>> *B.Sc. (Hons.) Agriculture*
> >>>>
> >>>>
> >>>>
> >>>> On Tue, Mar 23, 2021 2:48 AM, Jim Lemon [hidden email] wrote:
> >>>>
> >>>>> Hi Goyani,
> >>>>>
> >>>>> I learned something, too, as you may have noticed.
> >>>>>
> >>>>>
> >>>>> Dr Jim Lemon
> >>>>>
> >>>>> Gladesville, NSW
> >>>>>
> >>>>> AUSTRALIA
> >>>>>
> >>>>>
> >>>>> I no longer have a university affiliation.
> >>>>>
> >>>>>
> >>>>> Jim
> >>>>>
> >>>>>
> >>>>> On Tue, Mar 23, 2021 at 3:43 AM Goyani Zankrut <[hidden email]>
> >>>>> wrote:
> >>>>>
> >>>>> >
> >>>>>
> >>>>> > Greetings of the day,
> >>>>>
> >>>>> > Sir, can you share your details so I can write them in my thesis
> >>>>> work. I want to write everyone's name and their designation &
> institute
> >>>>> name. If you are not belonging to any institute, I mentioned your
> origin
> >>>>> (City, state, Country).
> >>>>>
> >>>>> > I hope you will send these details.
> >>>>>
> >>>>> > Again thanks for the help.
> >>>>>
> >>>>> > "Healthy soil, Healthy life."
> >>>>>
> >>>>> > "A war based on Satyagraha is always of two kinds. One is the war
> we
> >>>>> wage against injustice, and the other we fight our won weaknesses." -
> >>>>> Sardar Patel
> >>>>>
> >>>>> > "You have to dream before your dreams can come true." - A. P. J.
> >>>>> Abdul Kalam
> >>>>>
> >>>>> > "Think before you print and save a tree."
> >>>>>
> >>>>> >
> >>>>>
> >>>>> > ZANKRUT GOYANI
> >>>>>
> >>>>> > B.Sc. (Hons.) Agriculture
> >>>>>
> >>>>>
> >>>>>
> >>>>
> >>>> *"Healthy soil, Healthy life."*
> >>>> *"A war based on Satyagraha is always of two kinds. One is the war we
> >>>> wage against injustice, and the other we fight our won weaknesses."*
> - *Sardar
> >>>> Patel*
> >>>> *"You have to dream before your dreams can come true."* - *A. P. J.*
> *Abdul
> >>>> Kalam*
> >>>> *"Think before you print and save a tree."*
> >>>>
> >>>> *ZANKRUT GOYANI*
> >>>> *B.Sc. (Hons.) Agriculture*
> >>>>
> >>>
>
>         [[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.
>

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