[R] strange behavior, adds new field by non-existent field

Bill.Venables at csiro.au Bill.Venables at csiro.au
Sat Mar 20 04:06:55 CET 2010


This is a property of lists.  With the "$" form of extraction names may be abbreviated to the minimum number of characters required to specify the component uniquely.  This with your tmp1

> tmp1$Q
[1] 1 2 3 4 5 6 7 9
Levels: 1 2 3 4 5 6 7 9
> tmp1$Q.
[1] 1 2 3 4 5 6 7 9
Levels: 1 2 3 4 5 6 7 9
> tmp1$Q.4
[1] 1 2 3 4 5 6 7 9
Levels: 1 2 3 4 5 6 7 9


If you were to do it another way, e.g.

> tmp1 <- within(tmp1, is.na(Q.4[!(Q.4 %in% c("1","5"))]) <- TRUE)

then you would need to use the full component name, Q.4.  (This works too, by the way:

> tmp1
      Date HrMn  Temp  Q.4
1 19450101  900 -37.0    1
2 19450101 1000 -35.9 <NA>
3 19450101 1100 -35.9 <NA>
4 19450101 1200 -36.4 <NA>
5 19450101 1300 -36.4    5
6 19450101 1400 -36.4 <NA>
7 19450101 1500 -36.4 <NA>
8 19450101 1600 -37.5 <NA>
> 

you may care to figure out how it does so, though!)
________________________________________
On Fri, Mar 19, 2010 at 3:38 PM, Peter Keller <> wrote:

>
>
> data:
> > tmp1
>      Date HrMn  Temp Q.4
> 1 19450101 0900 -37.0   1
> 2 19450101 1000 -35.9   2
> 3 19450101 1100 -35.9   3
> 4 19450101 1200 -36.4   4
> 5 19450101 1300 -36.4   5
> 6 19450101 1400 -36.4   6
> 7 19450101 1500 -36.4   7
> 8 19450101 1600 -37.5   9
>
> Accidentally, I did this (I meant to write Q.4 instead of Q here)
> > tmp1$Q[tmp1$Q!="1" & tmp1$Q!= "5"]<-NA
>
> I would have expected it to tank, but still got a new field with the the
> values I wanted
> > tmp1
>      Date HrMn  Temp Q.4    Q
> 1 19450101 0900 -37.0   1    1
> 2 19450101 1000 -35.9   2 <NA>
> 3 19450101 1100 -35.9   3 <NA>
> 4 19450101 1200 -36.4   4 <NA>
> 5 19450101 1300 -36.4   5    5
> 6 19450101 1400 -36.4   6 <NA>
> 7 19450101 1500 -36.4   7 <NA>
> 8 19450101 1600 -37.5   9 <NA>
>
> Can someone explain this?
> Peter
>
> > sessionInfo()
> R version 2.10.1 Patched (2010-03-16 r51294)
> i386-pc-mingw32
>
> locale:
> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
> States.1252
> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] lattice_0.18-3 fortunes_1.3-7 zoo_1.6-2
>
> loaded via a namespace (and not attached):
> [1] grid_2.10.1  tools_2.10.1
>
> --
> View this message in context:
> http://n4.nabble.com/strange-behavior-adds-new-field-by-non-existent-field-tp1603563p1603563.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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]]

______________________________________________
R-help at r-project.org mailing list
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.



More information about the R-help mailing list