Simon Kiss
2014-Jul-25 19:34 UTC
Turn Rank Ordering Into Numerical Scores By Transposing A Data Frame
Hello:
I have data that looks like mydf, below. It is the results of a survey where
participants were to put a number of statements (in this case colours) in their
order of preference. In this case, the rank number is the variable, and the
factor level for each respondent is which colour they assigned to that rank. I
would like to find a way to effectively transpose the data frame so that it
looks like mydf2, also below, where the colours the participants were able to
choose are the variables and the variable score is what that person ranked that
variable.
Ultimately what I would like to do is a factor analysis on these items, so
I'd like to be able to see if people ranked red and yellow higher together
but ranked green and blue together lower, that sort of thing.
I have played around with different variations of t(), melt(), ifelse() and if()
but can't find a solution.
Thank you
Simon
#Reproducible code
mydf<-data.frame(rank1=sample(c('red', 'blue',
'green', 'yellow'), replace=TRUE, size=100),
rank2=sample(c('red', 'blue', 'green',
'yellow'), replace=TRUE, size=100), rank3=sample(c('red',
'blue', 'green', 'yellow'), replace=TRUE, size=100),
rank4=sample(c('red', 'blue', 'green',
'yellow'), replace=TRUE, size=100))
mydf2<-data.frame(red=sample(c(1,2,3,4),
replace=TRUE,size=100),blue=sample(c(1,2,3,4),
replace=TRUE,size=100),green=sample(c(1,2,3,4), replace=TRUE,size=100)
,yellow=sample(c(1,2,3,4), replace=TRUE,size=100))
*********************************
Simon J. Kiss, PhD
Assistant Professor, Wilfrid Laurier University
73 George Street
Brantford, Ontario, Canada
N3T 2C9