简体   繁体   English

在数据框中移动列的最有效方法是什么

[英]what is the most efficient way to move a column in a dataframe

I want to move a column on the right to some place on the left of the data frame. 我想将右侧的列移动到数据框左侧的某个位置。 Since I am moving only one column and I have many columns. 由于我只移动一列,所以我有很多列。 I think reordering like this df <- df[,c("a","b","d","c")] won't be efficient. 我认为像这样的df <-df [,c(“ a”,“ b”,“ d”,“ c”)]]重新排序不会有效。 Since the dataframe contains many rows, I want to minimize rewriting things 由于数据框包含许多行,所以我想尽量减少重写

from: 从:

 name var1 var2 var3 var4 var5 ... varN
 a     1    1    1    1    1        1
 b     1    1    1    1    1        1
 c     1    1    1    1    1        1

to: 至:

  name var1 varN var2 var3 var4 ... varN-1
   a     1    1    1    1    1        1
   b     1    1    1    1    1        1
   c     1    1    1    1    1        1

You can use a vector of column indices rather than a vector of column names, so you can take advantage of sequence notation, like so: 您可以使用列索引的向量而不是列名称的向量,因此可以利用序列表示法,如下所示:

my_seq = c(1,ncol(df),2:(ncol(df)-1))
df[,my_seq]

For example, if your dataframe has 17 columns, we get: 例如,如果您的数据框有17列,我们将得到:

> my_seq
 [1]  1 17  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16

You can get there with append : 您可以使用append到达那里:

df <- data.frame(name=letters[1:5],var1=1,var2=1,var3=1,var4=1,var5=1)

# using names
df[append(names(df)[-ncol(df)], names(df)[ncol(df)], after=2)]

# using positions
df[append(seq(ncol(df)-1), ncol(df), after=2)]

#  name var1 var5 var2 var3 var4
#1    a    1    1    1    1    1
#2    b    1    1    1    1    1
#3    c    1    1    1    1    1
#4    d    1    1    1    1    1
#5    e    1    1    1    1    1

I would recommend looking at the moveMe function from my "SOfun" package . 我建议从“ SOfun”包中查看moveMe函数

With it, the solution would be something like: 有了它,解决方案将类似于:

df <- data.frame(name=letters[1:5],var1=1,var2=1,var3=1,var4=1,var5=1)

library(SOfun)

df[moveMe(names(df), "var5 before var4")]
#   name var1 var2 var3 var5 var4
# 1    a    1    1    1    1    1
# 2    b    1    1    1    1    1
# 3    c    1    1    1    1    1
# 4    d    1    1    1    1    1
# 5    e    1    1    1    1    1

You can also compound statements: 您还可以复合语句:

df[moveMe(names(df), "var5 before var2; name last")]
#   var1 var5 var2 var3 var4 name
# 1    1    1    1    1    1    a
# 2    1    1    1    1    1    b
# 3    1    1    1    1    1    c
# 4    1    1    1    1    1    d
# 5    1    1    1    1    1    e

If you want to do this most efficiently, you should consider converting your data to a "data.table", and using setcolorder . 如果要最有效地执行此操作,则应考虑将数据转换为“ data.table”,并使用setcolorder This would change the column order by reference, and not by making copies of your data. 这将通过引用而不是通过复制数据来更改列顺序。

library(data.table)
dt <- as.data.table(df)

setcolorder(dt, moveMe(names(dt), "var5 before var4"))
dt
#    name var1 var2 var3 var5 var4
# 1:    a    1    1    1    1    1
# 2:    b    1    1    1    1    1
# 3:    c    1    1    1    1    1
# 4:    d    1    1    1    1    1
# 5:    e    1    1    1    1    1

dplyr

df %>% select(name,var1,varN,everthing())

If data frame df has n columns and you have to move m th column to 2nd position from start 如果数据帧dfn列,并且您必须从开始将第m列移到第二个位置

df <- subset(df, select=c(1, m, 2:m-1, m+1:n))

In your case: 在您的情况下:

df <- subset(df, select=c(name:var1, varN, var2:varN-1))

It can also be written as : 它也可以写成:

df <- subset(df, select=c(name, var1, varN, var2, var3,....,varN-1))

You can use columns names as well as column numbers for passing the new order of columns. 您可以使用列名和列号来传递新的列顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加作为时间序列数据帧中重复数字的二进制指示符的列的最有效方法是什么? - What is the most efficient way to add a column that is a binary indicator of a recurring number in time series dataframe? R:选择数据帧中某些行的最有效方法是什么 - R: What is the most efficient way to select certain rows in a dataframe 在R中分区和访问数据帧行的最有效方法是什么? - What's the most efficient way to partition and access dataframe rows in R? 更正列中文本类型数据的最有效方法是什么? - What is the most efficient way to correct text type data in a column? 在 R 中粘贴字符串的最有效方法是什么? - What is the most efficient way to paste strings in R? 在匹配来自第二个表的数据时,在`data.table`中创建向量列的最有效方法是什么? - What is the most efficient way to create a column of vectors in `data.table` when matching data from a second table? 索引嵌套列表/环境的有效(或最有效)方法是什么? - What's an efficient (or the most efficient) way to index nested lists/environments? 替换R中数据框中最低列表值的最有效方法 - Most efficient way to replace lowest list values in dataframe in R 通过R中较大数据帧的索引对元素进行子集化的最有效方法 - The most efficient way to subset elements by indexes from larger dataframe in R 将数据和元数据解析为相应数据帧的最有效方法 - Most efficient way to parse data & meta data into corresponding dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM