Routine matrix functions in ff

56 views Asked by At

I am new to dealing with big matrices in R. I am trying to learn with ff. I can create large ff matrices ffsdist1 and ffsdist2 as follows.

library(stringdist)
library(babynames)
library(ff)

d <- babynames
sdist1 <- stringdistmatrix(d$name[1:1000], d$name[1:2500], method="lv", useBytes = T)
sdist2 <- stringdistmatrix(d$name[1001:2000], d$name[1:2500], method="lv", useBytes = T)

I can create the ff matrices as follows

ffsdist1 <- ff(sdist1, dim=dim(sdist1))
ffsdist2 <- ff(sdist2, dim=dim(sdist2))

I would like to replicate the following operations on sdist1 and sdist2 in the ff matrices.

1. rbind()

sdist <- rbind(sdist1, sdist2)

2. Replace parts using [

sdist1[sdist1 > 0] <- Inf

3. Extract parts using[

ind1 <- grepl("\\bA", d$name[1:1000])
ind2 <- grepl("a\\b", d$name[1:1000])
sdist[ind1 & ind2]

and

sdist[,which(ind1)]

4. Add two matrices

sdistsum <- sdist1 + sdist2

5. Get transpose

tsdist1 <- t(sdist1)

Is this correct

tffsdist1 <- t(ffsdist1)

6. Use apply

apply(sdist, 1,
      function(x) paste(as.character(unlist(d$name[x <= 3])),
                        collapse=", "))

How to do this using ff package in R?

0

There are 0 answers