single-column data frame

[This article was first published on One Tip Per Day, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

To leave a comment for the author, please follow the link and comment on their blog: One Tip Per Day.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)