0

I have

diag(1:3)

#      [,1] [,2] [,3] 
# [1,]    1    0    0 
# [2,]    0    2    0 
# [3,]    0    0    3

and I need

matrix(data=c(0,1,0,0,2,0,0,0,3),nrow=3,ncol=3)

#      [,1] [,2] [,3] 
# [1,]    0    0    0 
# [2,]    1    2    0 
# [3,]    0    0    3

And I have to repeat this operation several times on a huge dataset.

Basically I need to move one observation from one line (3 in my dataset) below.

From what I know about R, I think I should find a condition and store it in a loop to run it on my huge dataset. I also thought about using the applyfunction but I do not know how.

I am very new on R, thus I want to apologize if my question sounds bad,or if it has already been ask and I want to thanks the R community for their knowledge.

M--
  • 25,431
  • 8
  • 61
  • 93
J. Perez
  • 117
  • 1
  • 2
  • 7
  • 3
    *Basically I need to move one observation from one line (3 in my dataset) below.* What do you mean exactly? – M-- Aug 16 '17 at 14:09
  • Also, it seems like the issue with your "huge dataset" might not be adequately addressed with a 3X3 matrix. Is there some repeated pattern involved in moving one observation? If yes, then please expand your example to address this pattern. (maybe a 6X3 or 6X6 matrix would be sufficient?). – lmo Aug 16 '17 at 14:12
  • Does this help? `library(dplyr);a <- diag(1:3);a[,1] <- lag(a[,1],default = 0);a` – M-- Aug 16 '17 at 14:12
  • I need to know how can I move one observation into a dataset. I want to be able to put this observation anywhere where I have NA's or 0's. – J. Perez Aug 16 '17 at 14:12
  • @J.Perez Based on your comment I can say for sure that this is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your actual dataset with what you want to do. Right now your are enforcing a solution about how to replace the `NA`s with the latest non-NA observation in a column or something like that. – M-- Aug 16 '17 at 14:15

0 Answers0