Sylvain Mareschal, Ph.D.
Bioinformatics engineer
October 9, 2013 at 17:17
moveColumn()
moveColumn.R (click to download)

This function moves a column into a data.frame next to an other one. It is mainly provided to make code more portable and clear when reorganizing data.frames to be exported.

Usage
source("http://bioinformatics.ovsa.fr/files/moveColumn.R")
moveColumn(dataframe, name, after, before)


Arguments
dataframe : The data.frame object to be reshaped.
name : Single character value, the name of the column to move.
after : Single character value, the targeted column will be moved just after the column whose name is provided here. Use NA to refer to the last column.
before : Single character value, the targeted column will be moved just before the column whose name is provided here. Use NA to refer to the first column. Ignored with a warning if after was also provided.

Value
Returns the provided data.frame, with columns switched.

Example
df = data.frame(abc=letters, ABC=LETTERS, N=1:26)[1:5,]
print(df)
moveColumn(df, "N", before="ABC")
moveColumn(df, "N", before=NA)
moveColumn(df, "abc", after="ABC")
moveColumn(df, "abc", after=NA)