2
$\begingroup$

I have a set of data created by users answering a questionnaire. I imported their answers from a .csv file and got them as a data frame with one user per row and one question per column.

However, the questions were not homogenous. I have to first evaluate some questions, which gives me an ordered list of the users' preferences for 10 predefined categories. Then I want to evaluate the remaining questions, and for this I have to use some information of this ordered list (for example, which is the category the user ranked highest?). I calculated the score for each category. It is currently kept in a matrix, which looks like that:

      cat1.score cat2.score    ...    cat10.score 
user1   2.50       2.25        ...     3.20
user2   3.85       2.05        ...     2.40

and I plan to create lists and sort them, so I'll get for the first user a list like preferences <- list("cat10", "cat1", "cat2", ...) (assuming that the scores not shown are lower than 2.25). But I am not sure how to structure the information. My plan was to create a new data frame, which will have the same data as the matrix, and its eleventh column will hold the list of the categories ranking for the user. I tried lots of ways to construct such a data frame, but couldn't do it.

Now I am very new to R, so I guess that I haven't thought of all ways yet, and I could try a lot more. But as I saw how hard it is to do, I guessed that maybe I am trying to do something which makes little sense - if it was a good practice, R would have probably had a convenient mechanism to do it, or the tutorial books would have had an example.

So, my question is, is this a good way to structure my intermediate results? And if not, what is a better way? I get one such list per user, and I really need it as ordered data (for each user, I will later have to access it as preferences[2] and get the category the user liked second most, or similar). To make it clear, I know which data structures in R can contain a list and which can't. My question is not what the language will let me do, but what is the sensible thing to do here.

$\endgroup$
3
$\begingroup$

I won't necessarily claim that this is the best way to organize your data, since if you decide down the road you want to do something different with it, in which case this may not be ideal.

But based on your stated desire to be able to easily extract a given user's preference, I might try simply building a list of each users sorted values:

#Build a toy data set similar to yours
m <- matrix(runif(100),10,10)
colnames(m) <- paste("cat",1:10,sep = "")
rownames(m) <- paste("user",1:10,sep = "")

#Build list of named, sorted vectors
catPref <- lapply(rownames(m),FUN = function(x){sort(m[x,])})
names(catPref) <- rownames(m)

Now the 4 highest score for user5 can be accessed like this:

catPref[['user5']][4]
    cat10 
0.3666077

So it was cat10 in this case. Frankly, though, rather than searching for the right data structure, I might just write a function that pulls this info directly from the matrix itself:

getPref <- function(dat,usr,ind){
    list(cat = colnames(dat)[rank(dat[usr,]) == ind],
            val = dat[usr,rank(dat[usr,]) == ind])
}

getPref(m,'user3',4)
$cat
[1] "cat6"

$val
[1] 0.3520883
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.