选择R中数据帧的最后n列 [英] Select the last n columns of data frame in R

查看:630
本文介绍了选择R中数据帧的最后n列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法系统地选择数据框架的最后一列?我希望能够将最后一列移动到第一列,但在移动列时保持列的顺序。我需要一种方式来执行此操作,而不是使用子集(数据,select = c(新订单中列出的所有列))列出所有列,因为我将使用许多不同的数据帧。



这里有一个例子,我想将最后2列移动到数据框的前面。它的作品,但它是丑陋的。

  A = rep(A,5)
B = rep(B,5)
num1 = c(1:5)
num2 = c(36:40)

mydata2 = data.frame(num1,num2,A,B)

#将A和B移动到mydata2的前面
mydata2_move = data.frame(A = mydata2 $ A,B = mydata2 $ B,mydata2 [,1:(ncol(mydata2)-2)])

#AB num1 num2
#1 AB 1 36
#2 AB 2 37
#3 AB 3 38
#4 AB 4 39
#5 AB 5 40

更改原始数据框中的列数会导致问题。这个工作(见下文),但命名被抛弃。为什么这两个例子的行为不同?有没有更好的方法来做这个,并把它推广一下?

  mydata1_move = data.frame(A = mydata1 $ A, B = mydata1 $ B,mydata1 [,1:(ncol(mydata1)-2)])

#AB mydata1 ... 1..ncol.mydata1 .... 2 ..
#1 AB 1
#2 AB 2
#3 AB 3
#4 AB 4
#5 AB 5
/ pre>

解决方案

你可以使用这样的东西:

  move_to_start<  -  function(x,to_move){
x [,c(to_move,setdiff(colnames(x),to_move))]
}

move_to_start(mydata2,c('A','B'))

#AB num1 num2
#1 AB 1 36
#2 AB 2 37
#3 AB 3 38
#4 AB 4 39
#5 AB 5 40

或者,如果要将最后一个 n 列移动到开头:

  move_to_start<  -  function(x,n){
x [,c(tail(seq_len(ncol(x))) n),seq_len(ncol(x) - n))]
}

move_to_start(mydata2,2)

#AB num1 num2
# 1 AB 1 36
#2 AB 2 37
#3 AB 3 38
#4 AB 4 39
#5 AB 5 40


Is there a way to systematically select the last columns of a data frame? I would like to be able to move the last columns to be the first columns, but maintain the order of the columns when they are moved. I need a way to do this that does not list all the columns using subset(data, select = c(all the columns listed in the new order)) because I will be using many different data frames.

Here's an example where I would like to move the last 2 columns to the front of the data frame. It works, but it's ugly.

A = rep("A", 5)
B = rep("B", 5)
num1 = c(1:5)
num2 = c(36:40)

mydata2 = data.frame(num1, num2, A, B)

# Move A and B to the front of mydata2
mydata2_move = data.frame(A = mydata2$A, B = mydata2$B, mydata2[,1:    (ncol(mydata2)-2)])

#  A B num1 num2
#1 A B    1   36
#2 A B    2   37
#3 A B    3   38
#4 A B    4   39
#5 A B    5   40

Changing the number of columns in the original data frame causes issues. This works (see below), but the naming gets thrown off. Why do these two examples behave differently? Is there a better way to do this, and to generalize it?

mydata1_move = data.frame(A = mydata1$A, B = mydata1$B, mydata1[,1:   (ncol(mydata1)-2)])

#  A B mydata1...1..ncol.mydata1....2..
#1 A B                                1
#2 A B                                2
#3 A B                                3
#4 A B                                4
#5 A B                                5

解决方案

You could use something like this:

move_to_start <- function(x, to_move) {
  x[, c(to_move, setdiff(colnames(x), to_move))]
} 

move_to_start(mydata2, c('A', 'B'))

#   A B num1 num2
# 1 A B    1   36
# 2 A B    2   37
# 3 A B    3   38
# 4 A B    4   39
# 5 A B    5   40

Alternatively, if you want to move the last n columns to the start:

move_to_start <- function(x, n) {
  x[, c(tail(seq_len(ncol(x)), n), seq_len(ncol(x) - n))]
} 

move_to_start(mydata2, 2)

#   A B num1 num2
# 1 A B    1   36
# 2 A B    2   37
# 3 A B    3   38
# 4 A B    4   39
# 5 A B    5   40

这篇关于选择R中数据帧的最后n列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆