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. HighCharts插件学习(二)

    HighCharts属性 credits: {版权信息} data: {数据功能模块} drilldown: {钻取} exporting: {导出}   labels: {标签}   legend: ...

  2. Java多线程-----原子变量和CAS算法

       原子变量      原子变量保证了该变量的所有操作都是原子的,不会因为多线程的同时访问而导致脏数据的读取问题      Java给我们提供了以下几种原子类型: AtomicInteger和Ato ...

  3. 【2017-04-17】类库、通用变量、is和as、委托

    类库dll文件,里边有很多被编译后的C#代码,不可阅读,不可修改,只能调用 1.类库创建 新建项目为类库,类库文件编写完成后,选择生成—生成解决方案,在debug文件夹下找到dll文件 2.类库引用 ...

  4. 设计模式之Strategy(策略)(转)

    Strategy是属于设计模式中 对象行为型模式,主要是定义一系列的算法,把这些算法一个个封装成单独的类. Stratrgy应用比较广泛,比如, 公司经营业务变化图, 可能有两种实现方式,一个是线条曲 ...

  5. linux常用命令:touch 命令

    linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1.命令格式: touch [选项]... 文件... 2.命令参数: -a    ...

  6. Java技术整理1---反射机制及动态代理详解

    1.反射是指在程序运行过程中动态获取类的相关信息,包括类是通过哪个加载器进行加载,类的方法和成员变量.构造方法等. 如下示例可以通过三种方法根据类的实例来获取该类的相关信息 public static ...

  7. GIT库代码管理规范

    GIT库代码管理规范 一. 规范要求 1. 每个项目建立单独的GIT库.每个GIT库包括两条线,命名规则如下: 开发线(测试):项目名称_DEV 生产线(正式):项目名称 2. 每条线只允许增量不允许 ...

  8. vue:vuex详解

    一.什么是Vuex? https://vuex.vuejs.org/zh-cn 官方说法:Vuex 是一个专为 Vue.js应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相 ...

  9. 自学Java第三个星期的总结

    在这一周里我在网上学习了java的分支结构.Number&Matht类.Character类.string类.String Buffer和String Builder类以及数组和日期时间等有关 ...

  10. Ubuntu下sublime-text3安装步骤

    1.在Ubuntu中按CTRL+ALT+T打开命令窗口,按下面步骤和命令进行安装即可: 添加sublime text 3的仓库: sudo add-apt-repository ppa:webupd8 ...