I am working in R for the first time and I have been having difficulty renaming column names in a dataframe (Grade.Data). I have a dataset imported from an csv file that has column names like this: Student.ID
Grade Interactive.Exercises.1..Health Interactive.Exercises.2..Fitness Quizzes.1..Week.1.Quiz Quizzes.2..Week.2.Quiz Case.Studies.1..Case.Study1 Case.Studies.2..Case.Study2 I would like to be able to change the variable names so that they are more simple, i.e. from Interactive.Exercises.1.Health to Interactive.Exercises.1 or Quizzes.1.Week.1.Quiz to Quizzes.1
So far, I have tried this:
grep(".*[0-9]", names(Grade.Data)) But I get this returned:
[1] 3 4 5 6 7 8 9 11 12 13 14 15 16 17 19 20 21 22 23 24 25 Can anyone help me figure out what is going on, and write a better regex expression? Thank you so much.
names(Grade.Data) <- sub("^(.*[^.])\\..*$", "\\1", names(Grade.Data)). What aboutCase.Studies.2..Case.Study2, what is the expected output? Also, try"^(.*[^.])\\.{2}.*"pattern.