R - transform upward diagonals to rows

你。 提交于 2019-12-24 05:03:01

问题


I am given a matrix, data frame or data table. I would like to create a matrix with the upward/reverse diagonals as rows and the remaining cells as NA. I was able to do this. But I think, there should be an easier and simpler solution. So any solutions are appreciated.

As an example, suppose I am given the following data.table

 df1<-as.data.table(matrix(seq(1:100),nrow=20, byrow = TRUE))

 > structure(df1)
     V1 V2 V3 V4  V5
  1:  1  2  3  4   5
  2:  6  7  8  9  10
  3: 11 12 13 14  15
  4: 16 17 18 19  20
  5: 21 22 23 24  25
  6: 26 27 28 29  30
  7: 31 32 33 34  35
  8: 36 37 38 39  40
  9: 41 42 43 44  45
 10: 46 47 48 49  50
 11: 51 52 53 54  55
 12: 56 57 58 59  60
 13: 61 62 63 64  65
 14: 66 67 68 69  70
 15: 71 72 73 74  75
 16: 76 77 78 79  80
 17: 81 82 83 84  85
 18: 86 87 88 89  90
 19: 91 92 93 94  95
 20: 96 97 98 99 100

> dput(df1)
structure(list(V1 = c(1L, 6L, 11L, 16L, 21L, 26L, 31L, 36L, 41L, 
46L, 51L, 56L, 61L, 66L, 71L, 76L, 81L, 86L, 91L, 96L), V2 = c(2L, 
7L, 12L, 17L, 22L, 27L, 32L, 37L, 42L, 47L, 52L, 57L, 62L, 67L, 
72L, 77L, 82L, 87L, 92L, 97L), V3 = c(3L, 8L, 13L, 18L, 23L, 
28L, 33L, 38L, 43L, 48L, 53L, 58L, 63L, 68L, 73L, 78L, 83L, 88L, 
93L, 98L), V4 = c(4L, 9L, 14L, 19L, 24L, 29L, 34L, 39L, 44L, 
49L, 54L, 59L, 64L, 69L, 74L, 79L, 84L, 89L, 94L, 99L), V5 = c(5L, 
10L, 15L, 20L, 25L, 30L, 35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 
75L, 80L, 85L, 90L, 95L, 100L)), row.names = c(NA, -20L), class = c("data.table","data.frame"), .internal.selfref = <pointer: 0x1038072e0>)

Then the following code does the job:

 df1 <- df1[nrow(df1):1,]
 df1 <- as.data.table(df1)
 n <- ncol(df1)
 df1 <- mapply(function(x, y) shift(x, n=y, type = "lead"), df1, seq_len(n)-1)
 df1 <- df1[nrow(df1):1,]

I then get the wanted result:

> structure(df1)
   V1 V2 V3 V4 V5
 [1,]  1 NA NA NA NA
 [2,]  6  2 NA NA NA
 [3,] 11  7  3 NA NA
 [4,] 16 12  8  4 NA
 [5,] 21 17 13  9  5
 [6,] 26 22 18 14 10
 [7,] 31 27 23 19 15
 [8,] 36 32 28 24 20
 [9,] 41 37 33 29 25
[10,] 46 42 38 34 30
[11,] 51 47 43 39 35
[12,] 56 52 48 44 40
[13,] 61 57 53 49 45
[14,] 66 62 58 54 50
[15,] 71 67 63 59 55
[16,] 76 72 68 64 60
[17,] 81 77 73 69 65
[18,] 86 82 78 74 70
[19,] 91 87 83 79 75
[20,] 96 92 88 84 80

structure(c(1L, 6L, 11L, 16L, 21L, 26L, 31L, 36L, 41L, 46L, 51L, 
56L, 61L, 66L, 71L, 76L, 81L, 86L, 91L, 96L, NA, 2L, 7L, 12L, 
17L, 22L, 27L, 32L, 37L, 42L, 47L, 52L, 57L, 62L, 67L, 72L, 77L, 
82L, 87L, 92L, NA, NA, 3L, 8L, 13L, 18L, 23L, 28L, 33L, 38L, 
43L, 48L, 53L, 58L, 63L, 68L, 73L, 78L, 83L, 88L, NA, NA, NA, 
4L, 9L, 14L, 19L, 24L, 29L, 34L, 39L, 44L, 49L, 54L, 59L, 64L, 
69L, 74L, 79L, 84L, NA, NA, NA, NA, 5L, 10L, 15L, 20L, 25L, 30L, 
35L, 40L, 45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L), .Dim = c(20L, 
5L), .Dimnames = list(NULL, c("V1", "V2", "V3", "V4", "V5")))

回答1:


An option would be to pass the n parameter of shift from 0 to 4 that corresponds to the each column in the Subset of Data.table (.SD)

df1[, Map(shift, .SD, n = 0:4)]
#     V1 V2 V3 V4 V5
# 1:  1 NA NA NA NA
# 2:  6  2 NA NA NA
# 3: 11  7  3 NA NA
# 4: 16 12  8  4 NA
# 5: 21 17 13  9  5
# 6: 26 22 18 14 10
# 7: 31 27 23 19 15
# 8: 36 32 28 24 20
# 9: 41 37 33 29 25
#10: 46 42 38 34 30
#11: 51 47 43 39 35
#12: 56 52 48 44 40
#13: 61 57 53 49 45
#14: 66 62 58 54 50
#15: 71 67 63 59 55
#16: 76 72 68 64 60
#17: 81 77 73 69 65
#18: 86 82 78 74 70
#19: 91 87 83 79 75
#20: 96 92 88 84 80

The type would be "lag" and not "lead" and by default it is the "lag"



来源:https://stackoverflow.com/questions/56118980/r-transform-upward-diagonals-to-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!