2

I'm having trouble setting up a sapply. I do have a for loop that will do the job I need it to, but it's taking too long to complete it.

variable names explained:

dat #raw data
df #empty data frame to preallocate memory
uniq.user #unique user id
uniq.item #unique item id

column names for df: user id, item id 1, item id 2, ..., item id n

I'm trying to create a binary table, indicating which item a user owns.
Example:

USERID1111 1 0 0 0 1
USERID2222 0 1 0 1 1

The raw data looks like this:

USERID1111 ITEM ID 1
USERID1111 ITEM ID 5
USERID2222 ITEM ID 2
USERID2222 ITEM ID 4
USERID2222 ITEM ID 5

The for loop I have is:

for(i in 1:length(uniq.user)){
    df[i, which(uniq.item %in% dat[df[i,1]== dat[,1], 2]) + 1] <- 1 
}

How would I convert this using sapply? (or any other apply functions)

Thank you!

p.s. If there are better ways to perform this task, please let me know! I'm trying to learn more efficient ways to do things in R.

chungsangh
  • 353
  • 1
  • 4
  • 15
  • Could someone give a brief explanation on why this was downvoted? I want to make sure I can frame the question better in the future. Thank you! – chungsangh Oct 11 '13 at 23:08
  • 1
    I certainly don't think it deserves a downvote, but, for next time, the nicest way to post data for an R questions is to use `dput`, that way people can copy/paste it into their R session. See [this question](http://stackoverflow.com/q/5963269/903061) for more details. – Gregor Thomas Oct 12 '13 at 00:17
  • ah, that's very good to know. Thank you – chungsangh Oct 13 '13 at 00:57

1 Answers1

3

Maybe table could be an alternative:

# some data
df <- data.frame(id = c(1, 1, 2, 2, 2), item = c(1, 5, 2, 4, 5))

# define possible levels of 'item', so that also levels with zero count appear in table
df$item <- factor(df$item, levels = 1:5)

# make table
with(df, table(id, item))
#     item
# id  1 2 3 4 5
#   1 1 0 0 0 1
#   2 0 1 0 1 1
Henrik
  • 65,555
  • 14
  • 143
  • 159
  • ah, the table function will do I think. Why is it wrapped in with()? Could you explain that part for me a little? – chungsangh Oct 11 '13 at 23:06
  • I am happy to explain `with`, but first I suggest you to have a look at the help text (`?with`). I feel I it would be unfair to such a nice function if I should try to explain it in just a few words in a comment. – Henrik Oct 11 '13 at 23:12
  • It's a pretty cool function. I'm not sure if I understand the difference between `with` and `within` – chungsangh Oct 11 '13 at 23:25
  • For a another quick example of `with`, check [here](http://stackoverflow.com/questions/1295955/what-is-the-most-useful-r-trick), 5th example from the top (a `within` example as well). – Henrik Oct 11 '13 at 23:26
  • that is very clear. Thanks for the help. I have a feeling I'll be using this a lot from now on :) – chungsangh Oct 11 '13 at 23:29
  • Glad to help. Just avoid `attach` and you will be fine...Good luck! – Henrik Oct 11 '13 at 23:30