R/insert_into.R

Defines functions insert_into

Documented in insert_into

#' Inserts a column into a dataframe
#' 
#' Inserts a column or dataframe y into a dataframe x at the 
#' specified position
#' 
#' @param x dataframe
#' @param y dataframe
#' @param where integer
#' 
#' @return dataframe
#' @export
#' @examples
#' df1<-data.frame(a=3,b=4,c=5)
#' df2<-data.frame(X=1,Y=2)
#' insert_into(df1, df2, where=1)
#' 

insert_into<-function(x,y,where=1){
  if (where==1){
    cbind(y,x)
  } else if (where>ncol(x)){
    cbind(x,y)
  } else {
    lhs<-1:(where-1)
    cbind(x[lhs],y,x[-lhs])
  }
}
chorscroft/myRPackage documentation built on Nov. 12, 2019, 9:44 p.m.