Rename columns in a data.frame, matrix, tibble, or GRanges object
Source:R/jamba-string.r
renameColumn.Rd
Rename columns in a data.frame, matrix, tibble, or GRanges object
Arguments
- x
data.frame
,matrix
,tbl
, orGRanges
equivalent object. It will work on any object for whichcolnames()
is defined.- from
character
vector of colnames expected to be inx
. Any values that do not matchcolnames(x)
are ignored.- to
character
vector withlength(to) == length(from)
corresponding to the target name for any colnames that matchfrom
.- verbose
logical
indicating whether to print verbose output.- ...
Additional arguments are ignored.
Value
data.frame
or object equivalent to the input x
,
with columns from
renamed to values in to
. For genomic
ranges objects such as GRanges
and IRanges
, the colnames
are updated in S4Vectors::values(x)
.
Details
This function is intended to rename one or more columns in a
data.frame
, matrix
, tibble, or GRanges
related object.
It will gracefully ignore columns which do not match,
in order to make it possible to call the
function again without problem.
This function will also recognize input objects GRanges
,
ucscData
, and IRanges
, which store annotation in DataFrame
accessible via S4Vectors::values()
. Note the IRanges
package
is required, for its generic function values()
.
The values supplied in to
and from
are converted from factor
to character
to avoid coersion by R to integer
, which was
noted in output prior to jamba version 0.0.72.900
.
See also
Other jam practical functions:
breakDensity()
,
call_fn_ellipsis()
,
checkLightMode()
,
check_pkg_installed()
,
colNum2excelName()
,
color_dither()
,
exp2signed()
,
getAxisLabel()
,
isFALSEV()
,
isTRUEV()
,
jargs()
,
kable_coloring()
,
lldf()
,
log2signed()
,
middle()
,
minorLogTicks()
,
newestFile()
,
printDebug()
,
reload_rmarkdown_cache()
,
rmInfinite()
,
rmNA()
,
rmNAs()
,
rmNULL()
,
setPrompt()
Examples
df <- data.frame(A=1:5, B=6:10, C=11:15);
df;
#> A B C
#> 1 1 6 11
#> 2 2 7 12
#> 3 3 8 13
#> 4 4 9 14
#> 5 5 10 15
df2 <- renameColumn(df,
from=c("A","C"),
to=c("a_new", "c_new"));
df2;
#> a_new B c_new
#> 1 1 6 11
#> 2 2 7 12
#> 3 3 8 13
#> 4 4 9 14
#> 5 5 10 15
df3 <- renameColumn(df2,
from=c("A","C","B"),
to=c("a_new", "c_new","b_new"));
df3;
#> a_new b_new c_new
#> 1 1 6 11
#> 2 2 7 12
#> 3 3 8 13
#> 4 4 9 14
#> 5 5 10 15