Tuesday, November 10, 2015

How to insert a column in between two columns

How to insert a column in between two columns in R?
    I have a column A | B, I want to add a column C in between A and B
I haven't been able to find an easy way to do it, but the following technique works.

Let us assume we have a data frame called 'data' and it has columns A and B. I want to add C in between A and C

First, you add a new column :
data$C <- 0
This command will create a new column with value 0

Then run,
data <- data[,"A","C","B"]
This would add C in between A and B.

This method is going to be extremely painful if the number of columns are more. I am still looking for a function to implement it and will update this post when I find one.  

No comments:

Post a Comment