[R] dataframes with only one variable

Richard M. Heiberger rmh at temple.edu
Thu Jan 12 14:49:00 CET 2006


> df1
  v1
1  1
2  2
3  3
4  4
> df1[,]
[1] 1 2 3 4
> df1[,1]
[1] 1 2 3 4
> df1[,,drop=F]
  v1
1  1
2  2
3  3
4  4
> df1[,1,drop=F]
  v1
1  1
2  2
3  3
4  4
> df1[1]
  v1
1  1
2  2
3  3
4  4
> df1[[1]]
[1] 1 2 3 4
> 


For transfers from Excel to R using the "[put/get] R dataframe" commands,
I think it is important always to use the drop=FALSE argument
(as I assume you are doing in RExcel V1.55).  The reason
for this is to maintain a rigid relationship between the only partially
compatible conventions of Excel and R.

For strictly within R use, the case is less clear.  I have trained myself 
always (well 85% on the first try) to use the drop=FALSE argument when I care
about the structure after the copy.

The tension between keeping the structure and demoting the structure predates
data.frames.  This was a design issue in matrices as well.
> tmp <- matrix(1:6,2,3)
> tmp
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
> tmp[1,]
[1] 1 3 5
> tmp[1,,drop=FALSE]
     [,1] [,2] [,3]
[1,]    1    3    5
> 

Rich




More information about the R-help mailing list