Monday, October 10, 2011

single-column data frame

This is a trivial but very useful tip:

> x=data.frame(a=1:4, c=5)
> x
  a c
1 1 5
2 2 5
3 3 5
4 4 5
> x[1,] 
  a c
1 1 5
> x[,1]
[1] 1 2 3 4
> x[,1, drop=F]
  a
1 1
2 2
3 3
4 4
where you can see that:
  • to avoid a[, i] become a vector, rather than a single-column data frame, we can use drop=F option. 
Here is full explanation for that: 
http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.data.frame.html

No comments:

Post a Comment