-1

I am new to R programming. For my course work, I am implementing Recommendation system using R. I already convert data table to matrix and then processed SVD=udv using irlba funtion. Now I have the following matrix u.

svd sign matrix of u

Now I need to classify them based on their sign. For example, here, first three are combination of(-, -) then last is (-,-), so they all must be in same community. Then 4th and 5th are (-, +), they are in same community and so on.

halfer
  • 19,824
  • 17
  • 99
  • 186
Anu
  • 211
  • 1
  • 9

2 Answers2

1

you can use sign on each column and paste them into your combination string. Then split the matrix into each combination

set.seed(0L)
mat <- matrix(rnorm(20), ncol=2)
split(data.frame(mat), apply(mat, 1, function(x) paste(sign(x), collapse=", ")))
#> $`-1, -1`
#>           X1         X2
#> 2 -0.3262334 -0.7990092
#> 6 -1.5399500 -0.4115108
#> 8 -0.2947204 -0.8919211
#> 
#> $`-1, 1`
#>             X1        X2
#> 7 -0.928567035 0.2522234
#> 9 -0.005767173 0.4356833
#> 
#> $`1, -1`
#>           X1         X2
#> 3  1.3297993 -1.1476570
#> 4  1.2724293 -0.2894616
#> 5  0.4146414 -0.2992151
#> 10 2.4046534 -1.2375384
#> 
#> $`1, 1`
#>         X1        X2
#> 1 1.262954 0.7635935
chinsoon12
  • 25,005
  • 4
  • 25
  • 35
  • Thank you. It is really helpful. – Anu Apr 24 '17 at 20:19
  • I have one more question in this result. After we split the result into several group, I need to collect the row name of each group as numeric and store (to store, I hope that list is better way). For example, need to keep track of (2,6,8), (7,9),(3,4,5,10),(1). Please help me to solve this. – Anu Apr 28 '17 at 11:38
  • You can use lapply(result, rownames) where result is the output from split – chinsoon12 Apr 28 '17 at 13:18
  • It's a great help. Thank you. I am totally new to R. So, please excuse me if I ask very basic questions. – Anu Apr 28 '17 at 16:46
  • No problem at all! – chinsoon12 Apr 28 '17 at 21:42
1

Another option is

lapply(split(seq_len(nrow(mat)), 
    interaction(as.data.frame(sign(mat)))), function(i) mat[i,, drop = FALSE])
akrun
  • 874,273
  • 37
  • 540
  • 662