1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
rename <- function(x,...,gsub=FALSE,fixed=TRUE,warn=TRUE){
m <- match.call(expand.dots=FALSE)
subst <- sapply(m$...,as.character)
if(gsub){
names.x <- names(x)
for(i in 1:length(subst)){
names.x <- gsub(names(subst[i]),subst[i],names.x,fixed=fixed)
}
names(x) <- names.x
}
else {
i <- match(names(subst),names(x))
if(any(is.na(i))) {
if(warn) warning("unused name(s) selected")
if(any(!is.na(i)))
subst <- subst[!is.na(i)]
i <- i[!is.na(i)]
}
if(length(i))
names(x)[i] <- subst
}
return(x)
}
dimrename <- function(x,dim=1,...,gsub=FALSE,fixed=TRUE,warn=TRUE){
m <- match.call(expand.dots=FALSE)
subst <- sapply(m$...,as.character)
if(0 %in% dim){
dimnames(x) <- rename(dimnames(x),...,gsub=gsub,fixed=fixed,warn=warn)
dim <- dim[dim!=0]
}
if(length(dim)){
for(i in 1:length(subst)){
for(j in dim){
if(gsub)
dimnames(x)[[j]] <- gsub(names(subst[i]),subst[i],dimnames(x)[[j]],fixed=fixed)
else{
ii <- match(names(subst[i]),dimnames(x)[[j]])
if(any(is.na(ii))) {
if(warn) warning("unused dimname(s) selected")
if(any(!is.na(ii)))
subst[i] <- subst[!is.na(ii)]
ii <- ii[!is.na(ii)]
}
if(length(ii))
dimnames(x)[[j]][ii] <- subst[i]
}
}
}
}
return(x)
}
colrename <- function(x,...,gsub=FALSE,fixed=TRUE,warn=TRUE){
m <- as.list(match.call())
m[[1]] <- as.name("dimrename")
m <- as.call(c(m[1:2],list(dim=2),m[-(1:2)]))
eval(m,envir=parent.frame())
}
rowrename <- function(x,...,gsub=FALSE,fixed=TRUE,warn=TRUE){
m <- as.list(match.call())
m[[1]] <- as.name("dimrename")
m <- as.call(c(m[1:2],list(dim=1),m[-(1:2)]))
eval(m,envir=parent.frame())
}
|