Say I have a data.frame:

df <- data.frame(A=c(10,20,30),B=c(11,22,33), C=c(111,222,333))
  A  B  C
1 10 11 111
2 20 22 222
3 30 33 333

If I select two (or more) columns I get a data.frame:

x <- df[,1:2]
   A  B
 1 10 11
 2 20 22
 3 30 33

This is what I want. However, if I select only one column I get a numeric vector:

x <- df[,1]
[1] 1 2 3

===================
Use drop=FALSE

> x <- df[,1, drop=FALSE]
> x
   A
1 10
2 20
3 30

From the documentation (see ?"[") you can find:

If drop=TRUE the result is coerced to the lowest possible dimension.
    
    
    
    
===================
Omit the ,:

x <- df[1]

A
1 10
2 20
3 30

From the help page of ?"[":

Indexing by [ is similar to atomic vectors and selects a list of the specified element(s).

A data frame is a list. The columns are its elements.

===================
REF:

https://stackoverflow.com/questions/21025609/how-do-i-extract-a-single-column-from-a-data-frame-as-a-data-frame

How do I extract a single column from a data.frame as a data.frame的更多相关文章

  1. SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'dtdate' 解决方法

    小微OAERR: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'mime' at row ...

  2. Spring SimpleJdbcTemplate Querying examples

    Here are few examples to show how to use SimpleJdbcTemplate query() methods to query or extract data ...

  3. Spring JdbcTemplate Querying examples

    Here are few examples to show you how to use JdbcTemplate query() methods to query or extract data f ...

  4. Getting started with machine learning in Python

    Getting started with machine learning in Python Machine learning is a field that uses algorithms to ...

  5. MySQL Developer

    1.The mysql Client Program 2.Data Types 3.Joins 4.Subqueries 5.Views 6.StoredRoutine . 1.Client/Serv ...

  6. Oracle composite index column ordering

    Question:  I have a SQL with multiple columns in my where clause.  I know that Oracle can only choos ...

  7. HBase在单Column和多Column情况下批量Put的性能对比分析

    作者: 大圆那些事 | 文章可以转载,请以超链接形式标明文章原始出处和作者信息 网址: http://www.cnblogs.com/panfeng412/archive/2013/11/28/hba ...

  8. [Hive - LanguageManual] Alter Table/Partition/Column

    Alter Table/Partition/Column Alter Table Rename Table Alter Table Properties Alter Table Comment Add ...

  9. How to fix Column 'InvariantName' is constrained to be unique 解决办法!

    Introduction When you build a web project that uses Enterprise Library Community for the Application ...

随机推荐

  1. python自定义安装包

    python的第三方模块越来越丰富,涉及的领域也非常广,如科学计算.图片处理.web应用.GUI开发等.当然也可以将自己写的模块进行打包或发布.一简单的方法是将你的类包直接copy到python的li ...

  2. html5-常用的3个基本标签

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  3. 斯坦福大学自然语言处理第四课“语言模型(Language Modeling)”

    http://52opencourse.com/111/斯坦福大学自然语言处理第四课-语言模型(language-modeling) 一.课程介绍 斯坦福大学于2012年3月在Coursera启动了在 ...

  4. OpenCV-3.3.0测试

    安装包目录下/samples/cpp里是各种例程 其中example_cmake里CMakeLists.txt已写好,直接cmake,make就可以,example.cpp是一个调用笔记本摄像头并显示 ...

  5. 20165305 苏振龙《Java程序设计》第九周学习总结

    第十三章 Java网络编程 学习了解用于网络编程的类,了解URL.Socket.InetAddress和DatagramSocket类在网络编程中的重要作用 使用URL创建对象的应用程序称作客户端程序 ...

  6. linux常用命令:rmdir 命令

    今天学习一下linux中命令: rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.)删 ...

  7. Map集合——双列集合

    双列集合<k, v> Map: Map 和 HashMap是无序的: LinkedHashMap是有序的: HashMap & LinkedHashMap: put方法: 其中,可 ...

  8. rgferg

    dfgsdfg fdvgdsafg fgdfgdfg

  9. JustOj 2042: Dada的游戏

    题目描述 Dada无聊时,喜欢做一个游戏,将很多钱分成若干堆排成一列,每堆钱数不固定,谁能找出每堆钱数严格递增的最长区间,谁就是人生赢家了.Dada可能脑子里的水还没干,她找不出来,你来帮她找找吧. ...

  10. Python Selenium 常用方法总结

    selenium Python 总结一些工作中可能会经常使用到的API. 1.获取当前页面的Url 方法:current_url  实例:driver.current_url    2.获取元素坐标 ...