2013-05-12 76 views
0

的我有列名2个的数据帧:重命名很多R列

print(colnames(selectedTrainData)) 
[1] "V331" "V305" "V310" "V161" "V322" "V271" "V355" "V83" "V185" "V10" 

print(colnames(selectedTestData)) 
[1] "V330" "V304" "V309" "V160" "V321" "V270" "V354" "V82" "V184" "V9" 

这里是在1 colnames之间的差异。如何重命名第一个数据框的colnames,使其在1处更少?

+1

'colnames(selectedTrainData)< - paste0(“V”,1:ncol(selectedTrainData))'或'colnames(selectedTrainData)< - colnames(selectedTestData)' – sgibb 2013-05-12 10:08:35

+0

不,它只是将colnames从1改为ncol TrainData) – Jane 2013-05-12 10:12:40

+0

2nd是有用的,thansk! – Jane 2013-05-12 10:13:53

回答

2

如果数字始终尾随元件,这工作:

txt <- colnames(selectedTrainData) 
r <- regexpr('\\d+', txt) 
colnames(selectedTrainData) <- 
    paste(
    substr(txt, 1, r-1), 
    as.numeric(substring(txt, r))-1, 
    sep='' 
) 
3

这里是一步一步的击穿

## first grab the column names as its own object 
nms <- colnames(selectedTestData) 

## second, strip out the starting "V" 
nms <- gsub("V", "", nms) 

## next, convert to a number 
nms <- as.numeric(nms) 

## substract 1 from each number 
nms <- nms - 1 

## The numbers are ready, now just paste the "V" back 
nms <- paste("V", nms, sep="") 

## Lastly, put the names back onto the original matrix or data.frame 
colnames(selectedTestData) <- nms 

Starting: 

    [1] "V330" "V304" "V309" "V160" "V321" "V270" "V354" "V82" "V184" "V9" 

Ending: 

    [1] "V329" "V303" "V308" "V159" "V320" "V269" "V353" "V81" "V183" "V8"