Problem

You want to do reorder the columns in a data frame.

Solution

# A sample data frame
data <- read.table(header=TRUE, text='
id weight size
1 20 small
2 27 large
3 24 medium
') # Reorder by column number
data[c(1,3,2)]
#> id size weight
#> 1 1 small 20
#> 2 2 large 27
#> 3 3 medium 24 # To actually change `data`, you need to save it back into `data`:
# data <- data[c(1,3,2)] # Reorder by column name
data[c("size", "id", "weight")]
#> size id weight
#> 1 small 1 20
#> 2 large 2 27
#> 3 medium 3 24

REF:

http://www.cookbook-r.com/Manipulating_data/Reordering_the_columns_in_a_data_frame/

Reordering the columns in a data frame的更多相关文章

  1. keep or remove data frame columns in R

    You should use either indexing or the subset function. For example : R> df <- data.frame(x=1:5 ...

  2. R语言 data.frame 大全

    A data frame is used for storing data tables. It is a list of vectors of equal length. For example, ...

  3. R class of subset of matrix and data.frame

    a = matrix(     c(2, 4, 3, 1, 5, 7), # the data elements     nrow=2,              # number of rows   ...

  4. How do I extract a single column from a data.frame as a data.frame

    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 ...

  5. R语言合并data.frame

    Merging Data Adding Columns To merge two data frames (datasets) horizontally,  use the merge functio ...

  6. R vs Python:构建data.frame、读取csv与统计描述

    一.Python 数据框就是典型的关系型数据库的数据存储形式,每一行是一条记录,每一列是一个属性,最终构成表格的形式,这是数据科学家必须熟悉的最典型的数据结构. 1.构建数据框 import pand ...

  7. R Data Frame

    https://www.datamentor.io/r-programming/data-frame/ Check if a variable is a data frame or not We ca ...

  8. AE开发能否实现TOC Control里添加多个Data Frame

    问题: 在ArcMap中,菜单Insert下Data Frame,可以在TOC中增加Data Frame,在MapControl或者PageLayoutControl下都可以正常显示多个Data Fr ...

  9. R语言Data Frame数据框常用操作

    Data Frame一般被翻译为数据框,感觉就像是R中的表,由行和列组成,与Matrix不同的是,每个列可以是不同的数据类型,而Matrix是必须相同的. Data Frame每一列有列名,每一行也可 ...

随机推荐

  1. HDU 1431 素数回文

    有人问我这个问题. 个人感觉暴搜会TLE O(n*sqrt(n)).n=100000000:(推断素数用2~sqrt(n)+1 去除) 还是枚举好了. 枚举 1~10000,把他每一位存下来,回文数已 ...

  2. scp拷贝文件

    有了亚马逊的ec2后,物美价廉,但是,亚马逊的aws使用密钥登陆的,命令和密码登录有一点不同.记录. 1.有密钥登陆,首先要把密钥文件 xxx.pem 的权限设为700,否则会报错. scp -i x ...

  3. 牛腩记账本core版本源码

    很简单的一个记账本项目,无非就是数据库的增删查改,采用vs2017 + asp.net core + mysql + dapper + layui, 其中访问mysql数据库用的是dapper, 界面 ...

  4. create-react-app的使用及原理分析

    create-react-app 是一个全局的命令行工具用来创建一个新的项目 react-scripts 是一个生成的项目所需要的开发依赖 一般我们开始创建react web应用程序的时候,要自己通过 ...

  5. GPG key retrieval failed

    Total size: 340 k Installed size: 1.2 M Is this ok [y/N]: y Downloading Packages: warning: rpmts_Hdr ...

  6. 行为类模式(六):备忘录(Memento)

    定义 在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. UML 优点 将被存储的状态放在外面,不要和关键对象混在一起,可以帮助维护内 ...

  7. 菜鸟学SSH(八)——Hibernate对象的三种状态

    前面写了几篇关于SSH的博客,但不是Struts就是Spring,Hibernate还从来没写过呢.说好是SSH的,怎么可以光写那两个,而不写Hibernate呢对吧.今天就先说说Hibernate对 ...

  8. cocos2d-x---CCLabelTTF加载字体库

    strPath = g_ImgPath + "方正卡通简体.ttf"; m_pLebGold = CCLabelTTF::create(); CC_ERROR(m_pLebGold ...

  9. (原创)一个简洁通用的调用DLL函数的帮助类

    本次介绍一种调用dll函数的通用简洁的方法,消除了原来调用方式的重复与繁琐,使得我们调用dll函数的方式更加方便简洁.用过dll的人会发现c++中调用dll中的函数有点繁琐,调用过程是这样的:在加载d ...

  10. python(58):python下划线

    详解Python中的下划线 本文将讨论Python中下划线(_)字符的使用方法.我们将会看到,正如Python中的很多事情,下划线的不同用法大多数(并非所有)只是常用惯例而已. 单下划线(_) 通常情 ...