[R] Unexpected behavior when giving a value to a new variable basedon the value of another variable

David McPearson dmcp at webmail.co.za
Sat Aug 30 05:54:20 CEST 2014


On Fri, 29 Aug 2014 06:33:01 -0700 Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote

> One clue is the help file for "$"...
> 
> ?" $"
> 
> In particular there see the discussion of character indices and the "exact"
> argument. 
>

<...snip...>
> 
> On August 29, 2014 1:53:47 AM PDT, Angel Rodriguez
> <angel.rodriguez at matiainstituto.net> wrote: >
> >Dear subscribers,
> >
> >I've found that if there is a variable in the dataframe with a name
<...sip...>
> >> N <- structure(list(V1 = c(67, 62, 74, 61, 60, 55, 60, 59, 58), V2 =
> >c(NA, 1, 1, 1, 1,1,1,1,NA)), 
> >+                     .Names = c("age","samplem"), row.names = c(NA,
> >-9L), class = "data.frame")
> >> N$sample[N$age >= 65] <- 1 
> >> N
> >  age samplem sample
> >1  67      NA      1
> >2  62       1      1
> >3  74       1      1
> >4  61       1      1
> >5  60       1      1
> >6  55       1      1
> >7  60       1      1
> >8  59       1      1
> >9  58      NA     NA
<...snip...>

Having seen all the responses about partial matching I almost understand. I've
also replicated the behaviour on R 2.11.1 so it's been around awhile. This
tells me it ain't a bug - so if any of the cognoscenti have the time and
inclination can someone give me a brief (and hopefully simple) explanation of
what is going on under the hood?

It looks (to me) like N$sample[N$age >= 65] <- 1 copies N$samplem to N$sample
and then does the assignment. If partial matching is the problem (which it
clearly is) my expectation is that  the  output should look like

   age samplem
1   67       1
2   62       1
3   74       1
4   61       1
5   60       1
6   55       1
7   60       1
8   59       1
9   58      NA
That is - no new column.
(and I just hate it when the world doesn't live up to my expectations!)

Bewildered and confused,
DMcP

____________________________________________________________
South Africas premier free email service - www.webmail.co.za 

Cotlands - Shaping tomorrows Heroes http://www.cotlands.org.za/



More information about the R-help mailing list