[R] Fwd: Datatable manipulation

arun smartpink111 at yahoo.com
Thu Nov 21 20:08:31 CET 2013



Hi,
Try:

dat1 <- read.table(text="a     b     c     d       e
1     2     3     4     5
10     9     8     7     6",sep="",header=TRUE)

Names1<- read.table(text="Original      New    
e     ee
b     bb    
a     aa
c     cc
d     dd",sep="",header=TRUE,stringsAsFactors=FALSE)

It is better to dput() your dataset.  For example:
 dput(Names1)
structure(list(Original = c("e", "b", "a", "c", "d"), New = c("ee", 
"bb", "aa", "cc", "dd")), .Names = c("Original", "New"), class = "data.frame", row.names = c(NA, 
-5L))


 names(dat1) <- Names1[,2][match(names(dat1), Names1[,1])] ##
 dat1
#  aa bb cc dd ee
#1  1  2  3  4  5
#2 10  9  8  7  6
A.K.





On Thursday, November 21, 2013 1:45 PM, Nitisha jha <nitisha999 at gmail.com> wrote:

Hi,

Thanks. I used as.character() and got the right strings.

Btw, I have lots of handicaps regarding R.

I have to rename the columns(I have 22 columns here). I have the new names along with the original names in another dataset. Right now, I am going hardcoding all the  19 name changes(tedious and not optimum). 1st 3 names remain the same. I will give u a sample dataset. Let me know if there is any easy way of doing this.  Pardon the displaced column labels.



Original dataset. 



   a b c d              
e 
1 2 3 4 5 
10 9 8 7 6 





Dataset for name change 


Original  New 



e ee 



b bb 



a aa 



c cc 



d dd 








I want my final dataset to be like this: 

aa bb cc dd ee 
1 2 3 4 5 
10 9 8 7 6 



 Could u tell me an optimal way to do it. My method is tedious and not good. 

Also, is there a way to import .xls without perl (windows)?


Thanks for being patient. :)



More information about the R-help mailing list