1

Suppose I have thousands of columns in "mydf". How can I cbind "new" column next to specific column name in "mydf". For example, I want to cbind it next to column "C" (by column name and not by number) and get the "Result".

mydf

A B C D
1 3 4 5 
1 3 3 2 
2 4 4 6
3 4 6 7
new
3
3
3
4

Result

A B C new D
1 3 4 3 5 
1 3 3 3 2 
2 4 4 3 6
3 4 6 4 7
MAPK
  • 5,635
  • 4
  • 37
  • 88
  • I don't want to list all the column names as it not feasible, I just need to append next to specific column. The link is not helpful. There is no way I can keep track of column numbers as well. I just know the specific column name and I want to cbind next to that column and the order of the column could change all the time, I need to do it by column name – MAPK Jul 09 '15 at 03:51
  • Search where is located your column to find the index. –  Jul 09 '15 at 04:00
  • I know I could do that by indexing, but is there any way I could append by name only? – MAPK Jul 09 '15 at 04:17
  • 1
    What kind of data do you have where it is truly infeasible to list the column names? Regardless, if you know the first column and the interesting column, then try `dplyr` with something like `cbind(select(mydf, A:C), newcolumn=newvector, select(mydf, -(A:C)))`. See [here](https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html#select-columns-with-select) for more info. – r2evans Jul 09 '15 at 06:06

0 Answers0