R iterating through 1600 cols in df with binary values 0 and 1 and copy values from two other columns to save in an array by group

I have a Dataframe df with 1600 columns named as X1,X2,X3....X1600 based on binary data 0 and 1 values and these 0 and 1 values have corresponding latitude and longitude data in first 2 columns. I have to iterate through each column of Dataframe with binary values one by one. Taking in to account 1st column I have to group it according to 0 and 1 values and their corresponding latitude and longitude values should be copied to a 2D array or list to be converted into a matrix at later stage.

Using a for loop would not be ideal, is there any simplified method to get this matrix x and y with 2 columns?

My df looks like this:

Latitude Longitude X1 X2 X3...
45.65 11.54 0 1 0
62.87 18.17 1 0 0
51.30 1.10 0 0 1

what I want to get on the basis of X1 is:

x:

Latitude Longitude
45.65 11.54
51.30 1.10

y:

Latitude Longitude
62.87 18.17

I need to continue this for all 1600 columns one by one. Any suggestions would be greatly appreciated.


Solution 1:

As I've explained in comments, I think this is a bad idea. But here is code to do it:

m = as.matrix(df[c("Latitude", "Longitude")])
results = lapply(df[-(1:2)], function(x) 
  list(
    x = m[x == 0, , drop = FALSE], 
    y = m[x == 1, , drop = FALSE]
  )
)
names(results) = names(df)[-(1:2)]

How I would suggest doing it instead (untested, probably not working code)

m = as.matrix(df[c("Latitude", "Longitude")])
cols = names(df)[-(1:2)]
results = list()
for(i in seq_along(cols)) {
  pea_result = peacock2(
    x = m[df[[cols[i]]] == 0, ], 
    y = m[df[[cols[i]]] == 1, ],
    ... # other args for peacock
  )
  results[[cols[i]]] = pea_result$pvalue
  ## alternately, you could make each item of results
  ## a sub-list that records more than just the p value
}