[R] Remove
David Winsemius
dwinsemius at comcast.net
Sat Dec 9 03:16:18 CET 2017
> On Dec 8, 2017, at 4:48 PM, Ashta <sewashm at gmail.com> wrote:
>
> Hi David, Ista and all,
>
> I have one related question Within one group I want to keep records
> conditionally.
> example within
> group A I want keep rows that have " x" values ranged between 15 and 30.
> group B I want keep rows that have " x" values ranged between 40 and 50.
> group C I want keep rows that have " x" values ranged between 60 and 75.
When you have a problem where there are multiple "parallel: parameters, the function to "reach for" is `mapply`.
mapply( your_selection_func, group_vec, min_vec, max_vec)
... and this will probably return the values as a list (of dataframes if you build the function correctly, so you may may need to then do:
do.call(rbind, ...)
--
David.
>
>
> DM <- read.table( text='GR x y
> A 25 125
> A 23 135
> A 14 145
> A 35 230
> B 45 321
> B 47 512
> B 53 123
> B 55 451
> C 61 521
> C 68 235
> C 85 258
> C 80 654',header = TRUE, stringsAsFactors = FALSE)
>
>
> The end result will be
> A 25 125
> A 23 135
> B 45 321
> B 47 512
> C 61 521
> C 68 235
>
> Thank you
>
> On Wed, Dec 6, 2017 at 10:34 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>>
>>> On Dec 6, 2017, at 4:27 PM, Ashta <sewashm at gmail.com> wrote:
>>>
>>> Thank you Ista! Worked fine.
>>
>> Here's another (possibly more direct in its logic?):
>>
>> DM[ !ave(DM$x, DM$GR, FUN= function(x) {!length(unique(x))==1}), ]
>> GR x y
>> 5 B 25 321
>> 6 B 25 512
>> 7 B 25 123
>> 8 B 25 451
>>
>> --
>> David
>>
>>> On Wed, Dec 6, 2017 at 5:59 PM, Ista Zahn <istazahn at gmail.com> wrote:
>>>> Hi Ashta,
>>>>
>>>> There are many ways to do it. Here is one:
>>>>
>>>> vars <- sapply(split(DM$x, DM$GR), var)
>>>> DM[DM$GR %in% names(vars[vars > 0]), ]
>>>>
>>>> Best
>>>> Ista
>>>>
>>>> On Wed, Dec 6, 2017 at 6:58 PM, Ashta <sewashm at gmail.com> wrote:
>>>>> Thank you Jeff,
>>>>>
>>>>> subset( DM, "B" != x ), this works if I know the group only.
>>>>> But if I don't know that group in this case "B", how do I identify
>>>>> group(s) that all elements of x have the same value?
>>>>>
>>>>> On Wed, Dec 6, 2017 at 5:48 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
>>>>>> subset( DM, "B" != x )
>>>>>>
>>>>>> This is covered in the Introduction to R document that comes with R.
>>>>>> --
>>>>>> Sent from my phone. Please excuse my brevity.
>>>>>>
>>>>>> On December 6, 2017 3:21:12 PM PST, David Winsemius <dwinsemius at comcast.net> wrote:
>>>>>>>
>>>>>>>> On Dec 6, 2017, at 3:15 PM, Ashta <sewashm at gmail.com> wrote:
>>>>>>>>
>>>>>>>> Hi all,
>>>>>>>> In a data set I have group(GR) and two variables x and y. I want to
>>>>>>>> remove a group that have the same record for the x variable in each
>>>>>>>> row.
>>>>>>>>
>>>>>>>> DM <- read.table( text='GR x y
>>>>>>>> A 25 125
>>>>>>>> A 23 135
>>>>>>>> A 14 145
>>>>>>>> A 12 230
>>>>>>>> B 25 321
>>>>>>>> B 25 512
>>>>>>>> B 25 123
>>>>>>>> B 25 451
>>>>>>>> C 11 521
>>>>>>>> C 14 235
>>>>>>>> C 15 258
>>>>>>>> C 10 654',header = TRUE, stringsAsFactors = FALSE)
>>>>>>>>
>>>>>>>> In this example the output should contain group A and C as group B
>>>>>>>> has the same record for the variable x .
>>>>>>>>
>>>>>>>> The result will be
>>>>>>>> A 25 125
>>>>>>>> A 23 135
>>>>>>>> A 14 145
>>>>>>>> A 12 230
>>>>>>>> C 11 521
>>>>>>>> C 14 235
>>>>>>>> C 15 258
>>>>>>>> C 10 654
>>>>>>>
>>>>>>> Try:
>>>>>>>
>>>>>>> DM[ !duplicated(DM$x) , ]
>>>>>>>>
>>>>>>>> How do I do it R?
>>>>>>>> Thank you.
>>>>>>>>
>>>>>>>> ______________________________________________
>>>>>>>> R-help at 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.
>>>>>>>
>>>>>>> David Winsemius
>>>>>>> Alameda, CA, USA
>>>>>>>
>>>>>>> 'Any technology distinguishable from magic is insufficiently advanced.'
>>>>>>> -Gehm's Corollary to Clarke's Third Law
>>>>>>>
>>>>>>> ______________________________________________
>>>>>>> R-help at 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.
>>>>>
>>>>> ______________________________________________
>>>>> R-help at 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.
>>>
>>> ______________________________________________
>>> R-help at 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.
>>
>> David Winsemius
>> Alameda, CA, USA
>>
>> 'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law
>>
>>
>>
>>
>>
David Winsemius
Alameda, CA, USA
'Any technology distinguishable from magic is insufficiently advanced.' -Gehm's Corollary to Clarke's Third Law
More information about the R-help
mailing list