MoveFront {DataCombine} | R Documentation |
Move variables to the front of a data frame.
Description
MoveFront
moves variables to the front of a data frame.
Usage
MoveFront(data, Var, exact = TRUE, ignore.case = NULL, fixed = NULL)
Arguments
data |
a data frame object containing the variable you want to move. |
Var |
a character vector naming the variables you would like to move to the front of the data frame. The order of the variables should match the order you want them to have in the data frame, i.e. the first variable in the vector will be the first variable in the data frame. |
exact |
logical. If |
ignore.case |
logical. If |
fixed |
logical. If |
Source
Based primarily on a Stack Overflow answer written by rcs: http://stackoverflow.com/questions/3369959/moving-columns-within-a-data-frame-without-retyping.
Examples
# Create fake data
A <- B <- C <- 1:50
OldOrder <- data.frame(A, B, C)
# Move C to front
NewOrder1 <- MoveFront(OldOrder, "C")
names(NewOrder1)
# Move B and A to the front
NewOrder2 <- MoveFront(OldOrder, c("B", "A"))
names(NewOrder2)
## Non-exact matching (example from Felix Hass)
# Create fake data
df <- data.frame(dummy = c(1,0), Name = c("Angola", "Chad"),
DyadName = c("Government of Angola - UNITA",
"Government of Chad - FNT"),
Year = c("2002", "1992"))
df <- MoveFront(df, c("Name", "Year"), exact = FALSE)
names(df)
df <- MoveFront(df, c("Name", "Year"), exact = TRUE)
names(df)