Skip to contents

Allows the user to choose precisely which two columns they want to swap places, while optionally preventing some rows within the columns from being altered in the process. Excluded rows within the columns act as anchors that are immune from the switching operation on the selected columns.

Usage

switch_cols(data, col1, col2, keep.rows = NULL)

Arguments

data

dataset object

col1

numeric or character the first column name or number

col2

numeric or character the second column name or number

keep.rows

numeric. row number to keep

Examples


# Example using mtcars
data101 <- mtcars[1:7,]

head(data101) # preview overall data
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

# task 1: basic result of switching columns 5 and 6
head(switch_cols(data101, 5, 6))
#>                    mpg cyl disp  hp    wt drat  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 2.620 3.90 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 2.875 3.90 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 2.320 3.85 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.215 3.08 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.440 3.15 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 3.460 2.76 20.22  1  0    3    1

# task 1: basic result of switching columns number 5 and name "gear"
head(switch_cols(data101, 5, "gear"))
#>                    mpg cyl disp  hp gear    wt  qsec vs am drat carb
#> Mazda RX4         21.0   6  160 110    4 2.620 16.46  0  1 3.90    4
#> Mazda RX4 Wag     21.0   6  160 110    4 2.875 17.02  0  1 3.90    4
#> Datsun 710        22.8   4  108  93    4 2.320 18.61  1  1 3.85    1
#> Hornet 4 Drive    21.4   6  258 110    3 3.215 19.44  1  0 3.08    1
#> Hornet Sportabout 18.7   8  360 175    3 3.440 17.02  0  0 3.15    2
#> Valiant           18.1   6  225 105    3 3.460 20.22  1  0 2.76    1

# task 1: basic result of switching columns "qsec" and "carb"
head(switch_cols(data101, "qsec", "carb"))
#>                    mpg cyl disp  hp drat    wt carb vs am gear  qsec
#> Mazda RX4         21.0   6  160 110 3.90 2.620    4  0  1    4 16.46
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875    4  0  1    4 17.02
#> Datsun 710        22.8   4  108  93 3.85 2.320    1  1  1    4 18.61
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215    1  1  0    3 19.44
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440    2  0  0    3 17.02
#> Valiant           18.1   6  225 105 2.76 3.460    1  1  0    3 20.22


# task 2: switch columns, but retain some rows with the switched columns


# lets exchange some columns, but keep content of row 4, 5 intact
data101[1:6,4:7] # preview the portion that is to be changed
#>                    hp drat    wt  qsec
#> Mazda RX4         110 3.90 2.620 16.46
#> Mazda RX4 Wag     110 3.90 2.875 17.02
#> Datsun 710         93 3.85 2.320 18.61
#> Hornet 4 Drive    110 3.08 3.215 19.44
#> Hornet Sportabout 175 3.15 3.440 17.02
#> Valiant           105 2.76 3.460 20.22
res1 <- switch_cols(data101, col1 = 5, col2 = 6, keep.rows = 4:5) # use column numbers
res1[1:6,4:7] # check result, pay attention to rows 4, 5 of columns 5, 6 as well
#>                    hp    wt  drat  qsec
#> Mazda RX4         110 2.620 3.900 16.46
#> Mazda RX4 Wag     110 2.875 3.900 17.02
#> Datsun 710         93 2.320 3.850 18.61
#> Hornet 4 Drive    110 3.080 3.215 19.44
#> Hornet Sportabout 175 3.150 3.440 17.02
#> Valiant           105 3.460 2.760 20.22

data101[1:6,6:11] # preview the portion that is to be changed
#>                      wt  qsec vs am gear carb
#> Mazda RX4         2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     2.875 17.02  0  1    4    4
#> Datsun 710        2.320 18.61  1  1    4    1
#> Hornet 4 Drive    3.215 19.44  1  0    3    1
#> Hornet Sportabout 3.440 17.02  0  0    3    2
#> Valiant           3.460 20.22  1  0    3    1
res2 <- switch_cols(data101,
col1 = "qsec",
col2 = "carb",
keep.rows = c(1,2,3)) # keep 1, 2, 3
res2[1:6,6:11] # check result
#>                      wt  carb vs am gear  qsec
#> Mazda RX4         2.620 16.46  0  1    4  4.00
#> Mazda RX4 Wag     2.875 17.02  0  1    4  4.00
#> Datsun 710        2.320 18.61  1  1    4  1.00
#> Hornet 4 Drive    3.215  1.00  1  0    3 19.44
#> Hornet Sportabout 3.440  2.00  0  0    3 17.02
#> Valiant           3.460  1.00  1  0    3 20.22