Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. # Get libraries
  2. library(data.table)
  3. library(ggplot2)
  4.  
  5. # Data table for demo
  6. data <- data.table("Bad names" = 1:100, "Shouldn't use spaces" = rep(c("text","other"),100))
  7.  
  8. # Store the original names
  9. # Changing this to c(names(data)) solves the problem but is a hack
  10. original.names <- names(data)
  11.  
  12. # Update the data's column names
  13. setnames(data, old = names(data),new = c("V1","V2"))
  14.  
  15. # Now original.names has also been changed to the new names
  16. # However, only the names of the data should have changed.
  17. cat(original.names,"n", names(data))
  18.  
  19. # Random ggplot
  20. ggplot(data) + geom_bar(aes(V1))
  21.  
  22. # Rename data
  23. setnames(data, names(data), original.names)
  24.  
  25. # I still get names V1 and V2, it is as if original names has been changed globally.
  26. cat(names(data))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement