0

I'm translating some matlab code to R, and specifically I am adding columns to a matrix, then rearrange them according to some index. Usually the matrix is large and does not encounter any issues, however, R does not recognize if an indexed range is illogical, and will go on to add columns as if I indexed in the reverse direction. See the code below for an example of what I mean. If the index is equal to 2, the new column is added as I hope. If the index is 3, however, R will interpret col_names[(index+1):(length(col_names)-1)]to mean col_names[4:3]and go on to re-add columns 4 and 3 from dummy_mat to the end (rather than an empty set). In my experience with matlab, indexing in reverse simply returns an empty set, which is why this algorithm worked originally. Is there some way to keep R from indexing in reverse? (I have tried various packages to do this, including arrange.vars to fix the issue of rearranging columns, but could not get it working properly. Also my question is specific to indexing backwards, not just rearranging the columns - this is the easiest example to produce). Thanks!

Working Example code:

# define dummy matrix
dummy_mat <-matrix(c(1:9),nrow = 3,ncol = 3)
colnames(dummy_mat) <- c('a','b','d')
rownames(dummy_mat) <- c('1','2','3')

#introduce new column to add at end
new_col_name <- 'c'
col_to_move <- matrix(0,nrow = nrow(dummy_mat),ncol = 1)
colnames(col_to_move) <- new_col_name

#add new column to end of dummy_mat
dummy_mat <- cbind(dummy_mat,col_to_move)

index <- 2 # want col_to_move to be repositioned to the right of the indexed column

#rearrange columns
col_names <- colnames(dummy_mat)

aug_mat <- dummy_mat[,c(col_names[1:index],new_col_name,col_names[(index+1):(length(col_names)-1)])] #move to correct location

aug_mat

Error-prone Example code:

# define dummy matrix
dummy_mat <-matrix(c(1:9),nrow = 3,ncol = 3)
colnames(dummy_mat) <- c('a','b','d')
rownames(dummy_mat) <- c('1','2','3')

#introduce new column to add at end
new_col_name <- 'c'
col_to_move <- matrix(0,nrow = nrow(dummy_mat),ncol = 1)
colnames(col_to_move) <- new_col_name

#add new column to end of dummy_mat
dummy_mat <- cbind(dummy_mat,col_to_move)

index <- 3 # want col_to_move to be repositioned to the right of the indexed column

#rearrange columns
col_names <- colnames(dummy_mat)

aug_mat <- dummy_mat[,c(col_names[1:index],new_col_name,col_names[(index+1):(length(col_names)-1)])] #move to correct location

aug_mat
Community
  • 1
  • 1
cbruno
  • 71
  • 1
  • 1
  • 8
  • Presumably you'll find some answers here: [Adding a column between two columns in a data.frame](http://stackoverflow.com/questions/13502601/adding-a-column-between-two-columns-in-a-data-frame) (matrices are similar) – MrFlick Apr 03 '17 at 22:07
  • @MrFlick Thanks, that should certainly help with the issue of adding columns. I still do have the issue of indexing backwards outside of the context of data manipulation like this. – cbruno Apr 03 '17 at 22:10
  • 2
    Then you can't use `:`. Write a function with whatever rule you want to use when making ranges. – MrFlick Apr 03 '17 at 22:11
  • Maybe something like this: http://stackoverflow.com/a/27907009/2372064 – MrFlick Apr 03 '17 at 22:13

0 Answers0