r - Turning a column into rows -
i have , example, data
rows <- c(1,1,1,1,1,1,1,1,1, 2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4,4, 5,5,5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6,6, 7,7,7,7,7,7,7,7,7, 8,8,8,8,8,8,8,8,8, 9,9,9,9,9,9,9,9,9) columns <- c(1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9) data <- c(1:81) dataset <- cbind(rows,columns) dataset <- cbind(dataset,data)
but these 3 columns. this:
rows columns data [1,] 1 1 1 [2,] 1 2 2 [3,] 1 3 3 [4,] 1 4 4 [5,] 1 5 5 [6,] 1 6 6
what need table looks this:, row-column being rownrs , column-column being columnnrs , data on it's respectful place
[,1][,2][,3][,4][,5][,6][,7][,8][,9] [1,] 1 2 3 4 5 6 7 8 9 [2,] 10 11 12 13 14 15 16 17 18 [3,] 19 20 21 22 23 24 25 26 27 [4,] 28 29 30 31 32 33 34 35 36 [5,] 37 38 39 40 41 42 43 44 45 [6,] 46 47 48 49 50 51 52 53 54 [7,] 55 56 57 58 59 60 61 62 63 [8,] 64 65 66 67 68 69 70 71 72 [9,] 73 74 75 76 77 78 79 80 81
is there way manipulate "dataset" table?
is there wrong just:
m <- matrix(1:81, ncol = 9, nrow = 9, byrow = true) rownames(m) <- colnames(m) <- 1:9 > m 1 2 3 4 5 6 7 8 9 1 1 2 3 4 5 6 7 8 9 2 10 11 12 13 14 15 16 17 18 3 19 20 21 22 23 24 25 26 27 4 28 29 30 31 32 33 34 35 36 5 37 38 39 40 41 42 43 44 45 6 46 47 48 49 50 51 52 53 54 7 55 56 57 58 59 60 61 62 63 8 64 65 66 67 68 69 70 71 72 9 73 74 75 76 77 78 79 80 81
or if data
special:
> dataset <- cbind(rows, columns, 1:81) > matrix(dataset[,3], ncol = max(dataset[, 2]), nrow = max(dataset[, 1]), + byrow = true) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 2 3 4 5 6 7 8 9 [2,] 10 11 12 13 14 15 16 17 18 [3,] 19 20 21 22 23 24 25 26 27 [4,] 28 29 30 31 32 33 34 35 36 [5,] 37 38 39 40 41 42 43 44 45 [6,] 46 47 48 49 50 51 52 53 54 [7,] 55 56 57 58 59 60 61 62 63 [8,] 64 65 66 67 68 69 70 71 72 [9,] 73 74 75 76 77 78 79 80 81
Comments
Post a Comment