Peter Keller
2010-Mar-19 19:38 UTC
[R] strange behavior, adds new field by non-existent field
data:> tmp1Date 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"]<-NAI would have expected it to tank, but still got a new field with the the values I wanted> tmp1Date 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.
jim holtman
2010-Mar-19 23:58 UTC
[R] strange behavior, adds new field by non-existent field
?'[' and the look for "partial matching" On Fri, Mar 19, 2010 at 3:38 PM, Peter Keller <kellerp.l@gmail.com> 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@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Jorge Ivan Velez
2010-Mar-20 02:02 UTC
[R] strange behavior, adds new field by non-existent field
Hi Peter, I think you want tmp1$Q.4[tmp1$Q.4 != 1 & tmp1$Q.4 != 5] <- NA 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 (note the ".4" after Q). HTH, Jorge 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@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]]
Bill.Venables at csiro.au
2010-Mar-20 03:06 UTC
[R] strange behavior, adds new field by non-existent field
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:> tmp1Date 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.