[R] Application of "merge" and "within"

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Tue May 31 20:46:06 CEST 2016


Then perhaps your example should illustrate one of these "many situations" that trouble you but you are not being clear about. 
-- 
Sent from my phone. Please excuse my brevity.

On May 31, 2016 11:39:04 AM PDT, Santosh <santosh2005 at gmail.com> wrote:
>I agree that performing merge outside the scope of "within" function,
>is
>pretty straight forward.. At times there are situations when many, if
>not
>all, of the operations are needed to be done within the scope the
>"within"
>environment..
>
>Thanks so much..
>Regards,
>Santosh
>
>On Tue, May 31, 2016 at 11:29 AM, Jeff Newmiller
><jdnewmil at dcn.davis.ca.us>
>wrote:
>
>> What is complicated about merge( q, r )?
>>
>> Keep in mind that there is nothing simple about the rules for
>non-standard
>> evaluation of variables that within() uses, and it only gets more
>> complicated if you try to apply those rules to two data frames at
>once.
>> While I am not quite sure I understand what you really want, I
>suspect you
>> won't like the behavior you get when you pile too much context into
>> within().
>>
>> Note that dplyr::inner_join, which is designed to fit into a whole
>> ecosystem of NSE functions, uses strings to specify column names to
>join by
>> just like the merge "by" parameters do rather than using NSE, because
>it is
>> actually the least confusing approach when two data frames are being
>> referenced.
>> --
>> Sent from my phone. Please excuse my brevity.
>>
>> On May 31, 2016 10:50:24 AM PDT, Santosh <santosh2005 at gmail.com>
>wrote:
>>>
>>> Thanks for response.. I want to merge two data frames using "within"
>>> function..the columns to used for merge could vary.. then the other
>>> commands become simpler..
>>>
>>> Thanks so much for your help!
>>> Santosh
>>>
>>> On Sat, May 28, 2016 at 1:53 PM, Duncan Murdoch
><murdoch.duncan at gmail.com>
>>> wrote:
>>>
>>>  On 27/05/2016 7:00 PM, Santosh wrote:
>>>>
>>>>  Dear Rxperts!
>>>>>
>>>>>  Is there a way to compute relative values.. using within()..
>function?
>>>>>
>>>>>  Any assistance/suggestions are highly welcome!!
>>>>>  Thanks again,
>>>>>  Santosh...
>>>>> ------------------------------
>>>>>
>>>>>  A sample dataset and the computation "outside" within()  function
>is
>>>>>  shown..
>>>>>
>>>>>  q <- data.frame(GL =
>>>>> rep(paste("G",1:3,sep = ""),each = 50),
>>>>>                  G  = rep(1:3,each = 50),
>>>>>                  D = rep(paste("D",1:5,sep = ""),each = 30),
>>>>>                  a = rep(1:15,each = 10),
>>>>>                  t = rep(seq(10),15),
>>>>>                  b = round(runif(150,10,20)))
>>>>>  r <- subset(q,!duplicated(paste(G,a)),sel=c(G,a,b))
>>>>>  names(r)[3] <- "bl"
>>>>>  s <- merge(q,r)
>>>>>   s$db <- s$b-s$bl
>>>>>
>>>>>  head(s,5)
>>>>>
>>>>>>
>>>>>>      G  a GL  D  t  b bl db
>>>>>  1   1  1 G1 D1  1 13 13  0
>>>>>  2   1  1 G1 D1  2 16 13  3
>>>>>  3   1  1 G1 D1  3 19 13  6
>>>>>  4   1  1 G1 D1  4 12 13 -1
>>>>>  5   1  1 G1 D1  5 19 13  6
>>>>
>>>>
>>>>
>>>>  Just use
>>>>
>>>>   s <- within(s, db <- b - bl)
>>>>
>>>>  Duncan Murdoch
>>>
>>>
>>>
>>>
>>>  [[alternative HTML version deleted]]
>>>
>>> ------------------------------
>>>
>>> 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.
>>>
>>>

	[[alternative HTML version deleted]]



More information about the R-help mailing list