码迷,mamicode.com
首页 > 其他好文 > 详细

Reordering the columns in a data frame

时间:2015-04-28 20:47:24      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/emanlee/p/4463859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!