marc@intershop.de
2005-Jan-12 11:50 UTC
[Rd] mydataframe$colname: using substring of colname may also match some column (PR#7496)
Full_Name: Marc Mamin Version: 1.8, 2.0.0 OS: Windows & Linux Submission from: (NULL) (217.17.202.254) Using only the beginning of a column name will match it:>aaa<-1 >df<-as.data.frame(aaa) >names(df)[1] "aaa">df$a[1] 1 !!!!!!!!! (I expect df$a to be undefind)>df$x >NULLCompare with:> df["aaa"]aaa 1 1> df["a"]Error in "[.data.frame"(df, "a") : undefined columns selected Here another example that underline how problematic this issue can be: aa1<-1 aa2<-2 df<-as.data.frame(aa1,aa2)> df$aa[1] 1 (only the first matching column is retrieved)
Prof Brian Ripley
2005-Jan-12 12:00 UTC
[Rd] mydataframe$colname: using substring of colname may also match some column (PR#7496)
That is the documented behaviour, not a bug!
BTW, df is the F density function.
If you want an exact match, use df[match("aa", names(df))]
On Wed, 12 Jan 2005 marc@intershop.de wrote:
> Full_Name: Marc Mamin
> Version: 1.8, 2.0.0
> OS: Windows & Linux
> Submission from: (NULL) (217.17.202.254)
>
>
> Using only the beginning of a column name will match it:
>
>> aaa<-1
>> df<-as.data.frame(aaa)
>> names(df)
> [1] "aaa"
>
>> df$a
> [1] 1 !!!!!!!!! (I expect df$a to be undefind)
Where did you read that?
>> df$x
>> NULL
>
> Compare with:
>
>> df["aaa"]
> aaa
> 1 1
>
>> df["a"]
> Error in "[.data.frame"(df, "a") : undefined columns
selected
>
>
> Here another example that underline how problematic this issue can be:
>
> aa1<-1
> aa2<-2
> df<-as.data.frame(aa1,aa2)
>> df$aa
> [1] 1 (only the first matching column is retrieved)
--
Brian D. Ripley, ripley@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
Tony Plate
2005-Jan-12 17:57 UTC
[Rd] mydataframe$colname: using substring of colname may also match some column (PR#7496)
At Wednesday 03:50 AM 1/12/2005, marc@intershop.de wrote:>Full_Name: Marc Mamin >Version: 1.8, 2.0.0 >OS: Windows & Linux >Submission from: (NULL) (217.17.202.254) > >[snipped issues previously responded to] > >Here another example that underline how problematic this issue can be: > >aa1<-1 >aa2<-2 >df<-as.data.frame(aa1,aa2) > > df$aa >[1] 1 (only the first matching column is retrieved)Actually, the above commands constructed a data frame with one column (the second argument to 'as.data.frame' is 'row.names'): > aa1<-1 > aa2<-2 > df <- as.data.frame(aa1,aa2) > df aa1 2 1 > dim(df) [1] 1 1 When a data frame (or list) does have columns whose names have common prefixes, the behavior is as documented (NULL is returned): > df2 <- data.frame(aa1, aa2) > df2 aa1 aa2 1 1 2 > df2$aa NULL >
Reasonably Related Threads
- Different behavior of model.matrix between R 3.2 and R3.1.1
- How to export multiple files using write.table in the loop?
- Different behavior of model.matrix between R 3.2 and R3.1.1
- Different behavior of model.matrix between R 3.2 and R3.1.1
- Call for testing: OpenSSH 7.2