[R] Bug in colnames of data.frames?
Marc Schwartz
MSchwartz at MedAnalytics.com
Tue Aug 17 16:34:08 CEST 2004
On Tue, 2004-08-17 at 09:01, Arne Henningsen wrote:
> Hi,
>
> I am using R 1.9.1 on on i686 PC with SuSE Linux 9.0.
>
> I have a data.frame, e.g.:
>
> > myData <- data.frame( var1 = c( 1:4 ), var2 = c (5:8 ) )
>
> If I add a new column by
>
> > myData$var3 <- myData[ , "var1" ] + myData[ , "var2" ]
>
> everything is fine, but if I omit the commas:
>
> > myData$var4 <- myData[ "var1" ] + myData[ "var2" ]
>
> the name shown above the 4th column is not "var4":
>
> > myData
> var1 var2 var3 var1
> 1 1 5 6 6
> 2 2 6 8 8
> 3 3 7 10 10
> 4 4 8 12 12
>
> but names() and colnames() return the expected name:
>
> > names( myData )
> [1] "var1" "var2" "var3" "var4"
> > colnames( myData )
> [1] "var1" "var2" "var3" "var4"
>
> And it is even worse: I am not able to change the name shown above the 4th
> column:
> > names( myData )[ 4 ] <- "var5"
> > myData
> var1 var2 var3 var1
> 1 1 5 6 6
> 2 2 6 8 8
> 3 3 7 10 10
> 4 4 8 12 12
>
> I guess that this is a bug, isn't it?
>
> Arne
Here is a hint:
# This returns an integer vector
> str(myData[ , "var1" ] + myData[ , "var2" ])
int [1:4] 6 8 10 12
# This returns a data.frame
> str(myData[ "var1" ] + myData[ "var2" ])
`data.frame': 4 obs. of 1 variable:
$ var1: int 6 8 10 12
> str(myData)
`data.frame': 4 obs. of 5 variables:
$ var1: int 1 2 3 4
$ var2: int 5 6 7 8
$ var3: int 6 8 10 12
$ var4:`data.frame': 4 obs. of 1 variable:
..$ var1: int 6 8 10 12
Take a look at the details, value and coercion sections of ?.data.frame
HTH,
Marc Schwartz
More information about the R-help
mailing list