Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

fill matrix with numbers in each row to create a parttern

$
0
0

I'm trying to generate a trip parttern using a matrix.

    Glastonbury Munich Venice Paris Ibiza Kamar-Taj
 [1,]           1      2      3     4     5         6
 [2,]           1      2      3     4     5         0
 [3,]           1      2      3     4     0         0
 [4,]           1      2      3     0     0         0
 [5,]           1      2      0     0     0         0
 [6,]           3      2      1     4     5         6
 [7,]           0      2      3     4     1         0
 [8,]           0      1      2     0     0         0
 [9,]           5      1      3     2     0         0

each row represents a single trip, and the number in a cell represents the order they visited each locations. Zero means they didn't visit that place.

currently I'm creating it like this:

tripMatrix <- list()

tripMatrix[[ 1 ]] <- c(1, 2, 3, 4, 5, 6)
tripMatrix[[ 2 ]] <- c(1, 2, 3, 4, 5, 0)
tripMatrix[[ 3 ]] <- c(1, 2, 3, 4, 0, 0)
tripMatrix[[ 4 ]] <- c(1, 2, 3, 0, 0, 0)
tripMatrix[[ 5 ]] <- c(1, 2, 0, 0, 0, 0)
tripMatrix[[ 6 ]] <- c(3, 2, 1, 4, 5, 6)
tripMatrix[[ 7 ]] <- c(0, 2, 3, 4, 1, 0)
tripMatrix[[ 8 ]] <- c(4, 5, 3, 2, 1, 0)
tripMatrix[[ 8 ]] <- c(0, 1, 2, 0, 0, 0)
tripMatrix[[ 10 ]] <- c(5, 1, 3, 2, 0, 0)

trips <- matrix(unlist(tripMatrix), ncol = 6, byrow = TRUE)

I can only do this for a few rows, but I'll like to generate N number of rows with different conbinations of places visited during a trip.

Please I can I do this for N number of rows without having to manually create a list of trips?


Viewing all articles
Browse latest Browse all 201919

Trending Articles