Skip to content Skip to sidebar Skip to footer

Iterate Permutation Per Row Per Item

I would like to manipulate data to do network analysis using ggnet. The dataset is in csv form and looks like this: offers {9425, 5801, 18451, 17958, 16023, 7166} {20003, 17737, 40

Solution 1:

Another R solution, assuming there are more rows in your file.

# read in csv file as list of integers (each row in csv = 1 list element)
offers <- readLines("offers.csv") %>% strsplit(",") %>% lapply(as.integer)

# create permutation pairs for each element in the list
permutation.list <- lapply(seq_along(offers), function(i) {t(combn(offers[[i]], m = 2))})

# combine all permutation pairs into 1 data frame
permutation.data.frame <- plyr::ldply(permutation.list, data.frame)

Below are the results based on the sample data provided:

>permutation.list
[[1]]
       [,1]  [,2]
 [1,]  9425  5801
 [2,]  9425 18451
 [3,]  9425 17958
 [4,]  9425 16023
 [5,]  9425  7166
 [6,]  5801 18451
 [7,]  5801 17958
 [8,]  5801 16023
 [9,]  5801  7166
[10,] 1845117958
[11,] 1845116023
[12,] 184517166
[13,] 1795816023
[14,] 179587166
[15,] 160237166

[[2]]
      [,1]  [,2]
[1,] 2000317737
[2,] 200034031
[3,] 200035554
[4,] 177374031
[5,] 177375554
[6,]  4031  5554

[[3]]
      [,1] [,2]
[1,] 197645553
[2,] 197645554
[3,]  5553 5554>permutation.data.frameX1X219425  580129425 1845139425 1795849425 1602359425  716665801 1845175801 1795885801 1602395801  716610184511795811184511602312184517166131795816023141795871661516023716616200031773717200034031182000355541917737403120177375554214031  55542219764555323197645554245553  5554

Solution 2:

you can try this in R:

a <- c(9425, 5801, 18451, 17958, 16023, 7166)
b <- c(20003, 17737, 4031, 5554)
c <- c(19764, 5553, 5554)

rbind(t(combn(a,2)),
t(combn(b,2)),
t(combn(c,2)))

Solution 3:

t(do.call(cbind,mapply(combn,list(a,b,c),2)))
       [,1][,2][1,]94255801[2,]942518451[3,]942517958[4,]942516023[5,]94257166[6,]580118451[7,]580117958[8,]580116023[9,]58017166[10,]1845117958[11,]1845116023[12,]184517166
  :     :      :
  :     :      :

Solution 4:

You already have the solutions for permutations. For breaking the array and merging it, open the csv read line by line and append to list.

from itertools import chain
import itertools
#Create Empty Dictionarylist= []
for i, eline inenumerate(CSVfile.readlines()):
    list.append(eline.strip())
MergedArray= {i for j in (list) for i in j}
#Use your permutations code belowprintlist(itertools.permutations(MergedArray, 2))

Post a Comment for "Iterate Permutation Per Row Per Item"