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