Vote count: 0
My dataset consists of survey responses at college with columns such as "Career" and "Q1.a", where the former is the college affiliation (such as "Med" or "Bus" or "UCol") and the latter records the participant's response to that specific question.
Given a variety of Career options, I would like to group them such that all careers that start with 'U' are grouped together to represent the undergraduates, while those that 'G' represent the graduates and the rest are other professional schools. The motivation is to see how these different groups have responded differently to various questions on the survey. I could not find a way to do this in-place so I created three different data-frames and modified them with the hope of merging them later. To this end, my naive approach has been:
careers <- as.character(df$Career)
Ucol <- df[startsWith(careers,'U'),]
Ucol$Career = "UCOL"
Grad <- df[startsWith(careers,'G'),]
Grad$Career = "GRAD"
Rest <- df[(!startsWith(careers, 'U') & !startsWith(careers, 'G')),]
Rest$Career = "PROF"
I was hoping I could combine Ucol, Grad and Rest together, but that doesn't seem to work. Specifically,
new_data <- rbind(Ucol, Grad, Rest)
But
summary(new_data)
only yields
Class :character
which is not helpful.
So, my question: How to group by condition, and then modify the names of those grouped column values?
R: Group by Condition + Modify Column Name
Aucun commentaire:
Enregistrer un commentaire