Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. x<-data.frame(matrix(data=rep("2",12),ncol=6))
  2. y<-data.frame(matrix(data=rep("3",12),ncol=6))
  3. z<-data.frame(matrix(data=rep("4",12),ncol=6))
  4. a<-list(x,y,z)
  5.  
  6. > a
  7. [[1]]
  8. X1 X2 X3 X4 X5 X6
  9. 1 2 2 2 2 2 2
  10. 2 2 2 2 2 2 2
  11.  
  12. [[2]]
  13. X1 X2 X3 X4 X5 X6
  14. 1 3 3 3 3 3 3
  15. 2 3 3 3 3 3 3
  16.  
  17. [[3]]
  18. X1 X2 X3 X4 X5 X6
  19. 1 4 4 4 4 4 4
  20. 2 4 4 4 4 4 4
  21.  
  22. >a
  23. [[1]]
  24. X1 Column2 Column3 Column4 Column5 Column6
  25. 1 2 2 2 2 2 2
  26. 2 2 2 2 2 2 2
  27.  
  28. [[2]]
  29. X1 Column2 Column3 Column4 Column5 Column6
  30. 1 3 3 3 3 3 3
  31. 2 3 3 3 3 3 3
  32.  
  33. [[3]]
  34. X1 X2 X3 X4 X5 X6
  35. 1 4 4 4 4 4 4
  36. 2 4 4 4 4 4 4
  37.  
  38. x<-data.frame(matrix(data=rep("2",12),ncol=6))
  39. y<-data.frame(matrix(data=rep("3",12),ncol=6))
  40. z<-data.frame(matrix(data=rep("4",12),ncol=6))
  41. a<-list(x,y,z)
  42.  
  43. data_frames_to_change<-c("x","y")
  44. library("data.table")
  45.  
  46. for (i in 1:length(data_frames_to_change)){
  47. setnames(eval(as.name(data_frames_to_change[i])),colnames(eval(as.name(data_frames_to_change[i]))[2:6]),c("Column2","Column3","Column4","Column5","Column6"))
  48. }
  49.  
  50. a<-list(x,y,z)
  51.  
  52. a[1:2] <- lapply(a[1:2], function(thisdf) {
  53. names(thisdf)[(length(thisdf)-4):length(thisdf)] <- paste0('Column',2:6)
  54. thisdf
  55. })
  56.  
  57. > a
  58. [[1]]
  59. X1 Column2 Column3 Column4 Column5 Column6
  60. 1 2 2 2 2 2 2
  61. 2 2 2 2 2 2 2
  62.  
  63. [[2]]
  64. X1 Column2 Column3 Column4 Column5 Column6
  65. 1 3 3 3 3 3 3
  66. 2 3 3 3 3 3 3
  67.  
  68. [[3]]
  69. X1 X2 X3 X4 X5 X6
  70. 1 4 4 4 4 4 4
  71. 2 4 4 4 4 4 4
  72.  
  73. colnames(a[[1]])<- c("X1","col2","col3","col4","col5","col6")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement