Search code examples
rfunctiondplyr

Functional programming with dplyr: column_to_rownames() inside function


I would like to use a function to set the first column of a dataframe as rownames. I tried to include column_to_rownames() inside a function. Here is my initial attempt (after consulting this article).

df <- tibble(a = c("a","a","a","b","b"), b= sample(1:10, 5))

col_to_row <- function(df, grp_var){
  require(dplyr)
  grp_var <- enquo(grp_var)

  df %>% column_to_rownames(var = quo_expr(grp_var))
}
col_to_row(df, a)

Which gives the error:

 Error in match(x, table, nomatch = 0L) : 'match' requires vector arguments 

I tried swapping quo_expr(grp_var) for !!grp_var:

col_to_row <- function(df, grp_var){
  require(dplyr)
  grp_var <- enquo(grp_var)

  df %>% column_to_rownames(var = !!grp_var)
}

Which gives the error:

 Error in is_quosure(e2) : argument "e2" is missing, with no default

I tried a solution with base R:

    col_to_row_base <- function(df, grp_var){
      df2 <- df[,-1]
      print(df2) # for debugging
      print(df[,1]) # for debugging
      row.names(df2) <- df[,1]
    }

Which gives error:

 Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length 

Any thoughts on how I can use a function to set the first column of a dataframe as rownames?

This is my first post here, so please let me know if this is not up to standards.

Running R version 3.5.2, Platform: x86_64-pc-linux-gnu (64-bit), Linux Mint 19, dplyr_0.8.0.1


Solution

  • Row names aren't really a good practice, but if you must do it:

    col_to_row_base <- function(df, names_col) {
        col_idx <- which(names(df) == names_col)
        df2 <- df[, -col_idx, drop = FALSE]
        row.names(df2) <- df[, col_idx]
        df2
    }
    

    Additionally, row names can't be duplicated, so that might be one of the reasons you got errors if you were trying to assign the values in column a as row names.