0

How I can swap 2 colmn of a data set in R? for example I have

 1  56
 2   43
 3   42
 4   32

and I want to have

   56    1
    43   2
    42   3
    32   4

3 Answers3

1

We can do the reverse sequence (generalized)

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

or for a two column, it is

df1[2:1]

If the OP wants to select only a particular column

df2 <- df1[c(6, 1:5)]

With tidyverse

library(dplyr)
df2 <- df1 %>%
        select(6, everything())
akrun
  • 874,273
  • 37
  • 540
  • 662
  • I have 6 column and I want to bring the last one as a first column –  Jun 02 '19 at 20:06
  • @user9272398 The first option would get it right or if you want to only take the last column first `df1[c(6, 1:5)]` – akrun Jun 02 '19 at 20:06
  • where I should write the name of dataset? –  Jun 02 '19 at 20:09
  • @user9272398 Lets say you read the dataset in `R` `df1 <- read.csv("yourfile.csv")` and here the code is working on that identifier – akrun Jun 02 '19 at 20:10
  • oh yes. when I write the data set to a txt file, there is a row in the begging before first column, do you know how I can remove it? I tried data[,-0] but it is not working. it is number of rows in " ". –  Jun 02 '19 at 20:16
  • 1
    @user9272398 If you are using `write.table`, `write.csv`, you can specify `row.names = FALSE` – akrun Jun 02 '19 at 20:17
  • thank you my last question for you : do you know if I can add this new column to data set : 1 0 0 0 1 0 0 0 1 ... basically one 1 and 3 zero –  Jun 02 '19 at 20:20
  • @user9272398 You can assign new column `df1$newColumn <- c(1, 0, 0, ....)` if i understand – akrun Jun 02 '19 at 20:21
  • new column is not 1 0 0 0 0 0 0 0 it is 1 0 0 0 1 0 0 0 1 0 0 0 1 . . . (one 1 then three 0 , one 1 then three 0 , ...) –  Jun 02 '19 at 20:24
  • 1
    @user9272398 You need `rep` i.e. `rep(rep(c(1, 0), c(1, 3)), length.out = nrow(df1))` – akrun Jun 02 '19 at 20:25
  • one more question: I have a column with false and true entries how I can change it to 0 and 1? –  Jun 02 '19 at 21:20
  • 1
    @user9272398 Just do `as.integer(df1$columnName)` or `+(df1$columnName)` – akrun Jun 02 '19 at 21:20
  • do I need a package? –  Jun 02 '19 at 21:23
  • @user9272398 No, it is `base R` – akrun Jun 02 '19 at 21:23
  • I get error: operator is invalid for atomic vectors –  Jun 02 '19 at 21:25
  • 1
    @user9272398 Do you have a `matrix` in that case `df1[, columnName]` where `columnName <- "stringName"` – akrun Jun 02 '19 at 21:25
  • it changed to 1 and 2 not 1 and 0 –  Jun 02 '19 at 21:28
  • 1
    @user9272398 Looks like you have a `factor` column. What is the result of `as.logical(df1[, columnName])`? Do you have as `TRUE/FALSE` or `True/False`. The upper case format is the one used in `R` for logical vector. If it is the second one, try `as.integer(df1[, columnName] == "True")` – akrun Jun 02 '19 at 21:32
  • 1
    @user9272398 In that case, it is `as.integer(df1[, columnName] == "yes")` – akrun Jun 02 '19 at 21:39
  • `df[2:1]` notation is nice in two-column dataframes, but not generalizable; i.e., if one has a three-column dataframe, `df[2:1]` removes the 3rd column. Hence, maybe, changing the positions in `c(...)` generalizable a little more. – Erdogan CEVHER Jun 03 '19 at 03:25
0

You can choose an arbitary order if you would like.

library(tidyverse)
df %>%
  select(col3,col4,col2,col1)
Bruno
  • 4,109
  • 1
  • 9
  • 27
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

You can swap by changing the locations within c (combine):

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
Erdogan CEVHER
  • 1,788
  • 1
  • 21
  • 40