如何更改R中两列的顺序?

时间:2019-06-02 20:02:50

标签: r database

我如何在R中交换数据集的2列?例如我有

 1  56
 2   43
 3   42
 4   32

我想拥有

   56    1
    43   2
    42   3
    32   4

3 个答案:

答案 0 :(得分:2)

我们可以进行反向序列化(概括)

USE master
sqlcmd -U texas -P texas -Q "drop table staging"
sqlcmd -U texas -P texas -Q "drop procedure sp_staging_old"
sqlcmd -U texas -P texas -Q "drop procedure sp_import"
sqlcmd -U texas -P texas -Q "drop procedure sp_import_from_old"
sqlcmd -U texas -P texas -Q "drop procedure sp_staging"
sqlcmd -U texas -P texas -Q "drop procedure sp_export"
sqlcmd -U texas -P texas -Q "drop procedure sp_import_no_TEX"
sqlcmd -U texas -P texas -Q "drop procedure sp_import_4_TEX"
sqlcmd -U texas -P texas -Q "drop procedure sp_staging_no_TEX"
sqlcmd -U texas -P texas -Q "drop procedure sp_staging_4_TEX"
sqlcmd -U texas -P texas -Q "drop procedure sp_export_no_TEX"
sqlcmd -U texas -P texas -Q "drop procedure sp_export_4_TEX"
sqlcmd -U texas -P texas -Q "drop procedure sp_import_no_TEX_Rel_4"
sqlcmd -U texas -P texas -Q "drop procedure sp_staging_no_TEX_Rel_4"
sqlcmd -U texas -P texas -Q "drop procedure sp_import_no_TEX_Rel_5"
sqlcmd -U texas -P texas -Q "drop procedure sp_staging_no_TEX_Rel_5"
sqlcmd -U texas -P texas -Q "drop procedure sp_import_4_TEX_Rel_5"
sqlcmd -U texas -P texas -Q "drop procedure sp_staging_4_TEX_Rel_5"
sqlcmd -U texas -P texas -Q "drop procedure sp_import_no_TEX_Rel_6"
sqlcmd -U texas -P texas -Q "drop procedure sp_staging_no_TEX_Rel_6"
sqlcmd -U texas -P texas -Q "drop procedure sp_import_4_TEX_Rel_6"
sqlcmd -U texas -P texas -Q "drop procedure sp_staging_4_TEX_Rel_6"
sqlcmd -U texas -P texas -d TEX -i sp_staging_old.sql
sqlcmd -U texas -P texas -d TEX -i sp_import.sql
sqlcmd -U texas -P texas -d TEX -i sp_import_from_old.sql
sqlcmd -U texas -P texas -d TEX -i sp_staging.sql
sqlcmd -U texas -P texas -d TEX -i sp_import_no_TEX.sql
sqlcmd -U texas -P texas -d TEX -i sp_import_no_TEX_Rel_4.sql
sqlcmd -U texas -P texas -d TEX -i sp_import_no_TEX_Rel_5.sql
sqlcmd -U texas -P texas -d TEX -i sp_import_no_TEX_Rel_6.sql
sqlcmd -U texas -P texas -d TEX -i sp_import_no_TEX_Rel_7.sql
sqlcmd -U texas -P texas -d TEX -i sp_import_4_TEX.sql
sqlcmd -U texas -P texas -d TEX -i sp_import_4_TEX_Rel_5.sql
sqlcmd -U texas -P texas -d TEX -i sp_import_4_TEX_Rel_6.sql
sqlcmd -U texas -P texas -d TEX -i sp_import_4_TEX_Rel_7.sql
sqlcmd -U texas -P texas -d TEX -i sp_staging_no_TEX_Rel_4.sql
sqlcmd -U texas -P texas -d TEX -i sp_staging_no_TEX_Rel_5.sql
sqlcmd -U texas -P texas -d TEX -i sp_staging_no_TEX_Rel_6.sql
sqlcmd -U texas -P texas -d TEX -i sp_staging_no_TEX_Rel_7.sql
sqlcmd -U texas -P texas -d TEX -i sp_staging_no_TEX.sql
sqlcmd -U texas -P texas -d TEX -i sp_staging_4_TEX.sql
sqlcmd -U texas -P texas -d TEX -i sp_staging_4_TEX_Rel_5.sql

或者对于两列,是

"Windows cannot access the specified device, path, or file. You may not 
have the appropriate permissions to access the item"

Also the following notification from antivirus program:
"A Process was blocked because malicious behaviour was detected"

如果OP仅希望选择特定列

merge

使用merge into t using (select t.*, row_number() over (partition by s.u_a_id, column_name order by line_num, e_id) as new_seq_id from t ) s on s.u_a_id = t.u_a_id and s.column_name = st.column_name when matched then update set t.my_seq = s.new_my_seq;

df2 <- df1[ncol(df1):1]

答案 1 :(得分:1)

您可以根据需要选择任意订单。

library(tidyverse)
df %>%
  select(col3,col4,col2,col1)

答案 2 :(得分:0)

df <- data.frame(c1 = 1:4, c2 = c(56, 43, 42, 32))
df
#  c1 c2
#1  1 56
#2  2 43
#3  3 42
#4  4 32

df[c(2,1)]
#  c2 c1
#1 56  1
#2 43  2
#3 42  3
#4 32  4

您可以通过更改c(合并)中的位置来进行交换:

df <- data.frame(c1=1:4, c2=c(56,43,42,32), c3=c(12,13,14,15));df
#  c1 c2 c3
#1  1 56 12
#2  2 43 13
#3  3 42 14
#4  4 32 15

df[c(3,1,2)]
#  c3 c1 c2
#1 12  1 56
#2 13  2 43
#3 14  3 42
#4 15  4 32