[R] Thanks for help
Jim Lemon
drj|m|emon @end|ng |rom gm@||@com
Thu Mar 25 22:55:41 CET 2021
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 <zankrut20 using gmail.com> 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 <zankrut20 using gmail.com> 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 <drjimlemon using gmail.com> 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 <zankrut20 using gmail.com>
>>> 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 drjimlemon using gmail.com 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 <zankrut20 using gmail.com>
>>>>> 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]]
More information about the R-help
mailing list