Search code examples
rrhdf5

Selecting pairs of numbers from a dataframe


I have a dataframe like this:

row   col
1     1
1     50
1     55
2     75

and I want to select all the row and col values that are paired so I can insert them into a function.

I am trying this code:

library(rhdf5)

 for(row in df$row){

  for (col in df$col){

      apixel = h5read("C/path_to_a_file',, index=list(1, col, row))
      apixel= data.frame(apixel)
 }
}

This is inserting all my row and col values from my df like I want, but it is doing every possible combination of them I only want to insert the appropriate pairing. For instance, row 1 should only ever be paired with 1, 50 and 55 as a corresponding col, not 75.

Possibly I could try to zip the two columns and then return the associated tuples?


Solution

  • First step: write the function so as it accepts pairs of (row, col) as an argument:

    read_pixels <- function(row, col) {
      require(rhdf5)
      apixel <- h5read("C/path_to_a_file',, index=list(1, col, row))
      apixel <- data.frame(apixel)
    
      # do something with apixel
    }
    

    Now you can pass each row of the data frame to your function. There are various ways to do that, here is an ugly one using apply:

    apply(myDataFrame, 1, function(x) read_pixels(x[1], x[2])