[R] Bug in colnames of data.frames?
Uwe Ligges
ligges at statistik.uni-dortmund.de
Tue Aug 17 16:22:50 CEST 2004
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" ]
This bug is the user ... ;-)
Type: str(myData)
`data.frame': 4 obs. of 3 variables:
$ var1: int 1 2 3 4
$ var2: int 5 6 7 8
$ var4:`data.frame': 4 obs. of 1 variable:
..$ var1: int 6 8 10 12
Aha! You have created a data.frame consisting of one column! What you
mean really mean is
myData$var5 <- myData[[ "var1" ]] + myData[[ "var2" ]]
Uwe Ligges
>
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list