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

时间:2015-11-03 22:53:54

标签: r

我想将右侧的列移动到数据框左侧的某个位置。因为我只移动一列,所以我有很多列。我想重新排序就像这样df< -df [,c(" a"," b"," d"," c") ]不会有效率。由于数据帧包含很多行,我想最小化重写事项

从:

 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

到:

  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

5 个答案:

答案 0 :(得分:2)

您可以使用列索引的向量而不是列名称的向量,因此您可以利用序列表示法,如下所示:

<?php

    require_once 'core/init.php';

    // Set user_id to the currently logged in user.
    $user_id = $user_data['id'];

    // Set $game_id to the current ID of the game (coming from ajax call)
    $game_id = $_POST['game_id'];

    // Grab the game (ID) from the games table, then Query it.
    //$SQL_SELECT_GAME = "select * from games where id = '$game_id'";
    //$db->query($SQL_SELECT_GAME);

    // Do an update on game_follow table, and set follow = 1 (means that this game is being followed) where the user_id = $user_id and game_id = $game_id, then Query it.
    $SQL_UPDATE = "update game_follows set follow = 1 where user_id = '$user_id' and game_id = '$game_id'";
    $db->query($SQL_UPDATE);

例如,如果您的数据框有17列,我们会得到:

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

答案 1 :(得分:2)

您可以使用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

答案 2 :(得分:2)

我建议您从the moveMe function查看my "SOfun" package

有了它,解决方案就像:

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

您还可以复合语句:

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

如果您希望最有效地执行此操作,则应考虑将数据转换为&#34; data.table&#34;,并使用setcolorder。这将通过引用更改列顺序,而不是通过复制数据。

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

答案 3 :(得分:0)

dplyr

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

答案 4 :(得分:0)

如果数据框dfn列,您必须将m列从开始移至第2位

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

在你的情况下:

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

它也可以写成:

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

您可以使用列名称和列号来传递新的列顺序。