Rename Dataframe Column Names in R using Previous Column Name and Regex Pattern -
i working in r first time , have been having difficulty renaming column names in dataframe (grade.data). have dataset imported csv file has column names 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 able change variable names more simple, i.e. interactive.exercises.1.health interactive.exercises.1 or quizzes.1.week.1.quiz quizzes.1
so far, have tried this:
grep(".*[0-9]", names(grade.data))
but returned:
[1] 3 4 5 6 7 8 9 11 12 13 14 15 16 17 19 20 21 22 23 24 25
can me figure out going on, , write better regex expression? thank much.
it seems truncate column names after first chunk of digits.
you may use following sub
solution:
names(grade.data) <- sub("^(.*?\\d+).*$", "\\1", names(grade.data))
see regex demo
details
^
- start of string(.*?\\d+)
- group 1 (later referred\1
replacement pattern) matching 0+ chars few possible (.*?
) , 1 or more digits (\d+
).*
- 0+ chars many possible$
- end of string
Comments
Post a Comment