Hi, I'm currently doing research and collecting a ranked-choice data. Basically people choosing their preferences in a topic.
E.g., people ranking their preference on fruits: orange, mango, apple, avocado
The part that I confused is to figure out how to give score for each column -> turn from semicolon separated string into column with numeric value.
If I can pass this, I can create the desired output dataframe.
I've found pmr package, but the documentation only a few. Moreover, this one too advance. I don't really need that for current state.
Here is an alternate solution using base R. It's not more efficient than Flm's solution. If anyone has an idea how I can make mine more efficient, probably with an apply statement, please let me know.
df1 <- data.frame((matrix(ncol=4,nrow=n)))
colnames(df1) <- c("4","3","2","1")
df2 <- data.frame((matrix(ncol=4,nrow=n)))
colnames(df2) <- c("apple","avocado","banana","orange")
for (i in 1:n){
df1[i,] <- unlist(strsplit(as.character(fruits[i]), split = ";"))
for (j in 1:4){
w <- which(df1[i,] == colnames(df2[j]))
c <- as.numeric(colnames(df1[w]))
df2[i,j] <- c
}
}
df2