switch_rows {quickcode} | R Documentation |
Switch the index of two rows in a data set
Description
Allows the user to choose precisely which two rows they want to swap places, while optionally preventing some columns from being altered in the process. Excluded columns within the rows act as anchors that are immune from the switching operation on the selected rows.
Usage
switch_rows(data, row1, row2, keep.cols = NULL)
Arguments
data |
dataset object |
row1 |
numeric. the first row number |
row2 |
numeric. the second row number |
keep.cols |
numeric or character. column number or name to keep |
Examples
# Example using mtcars
data100 <- mtcars[1:7,]
head(data100) # preview overall data
# task 1: basic result of switching rows 5 and 6
head(switch_rows(data100, 5, 6))
# task 2: switch rows, but retain some columns
data100[5:6,2:10] # preview the portion that is to be changed
# lets switch 2 rows, but keep content of columns 7, 8, 9 10 within the changed rows
res1 <- switch_rows(data100, row1 = 5, row2 = 6, keep.cols = 7:10) # use column numbers
res1[5:6,] # check result, pay attention to columns 9 and 10 as well
res2 <- switch_rows(data100,
row1 = 5,
row2 = 6,
keep.cols = c("disp","cyl")) # use column names
res2[5:6,] # check result, pay attention to columns "disp","cyl" as well
[Package quickcode version 0.9.1 Index]