Search code examples
rtidyversemutate

Unable to specify column names with space


Although this post showed that one could use backtick ` to select columns that had space in their names, I couldn't do that in the following code

library(tidyverse)
library(survival)

df <- colon[colon$etype==2, c("time", "age")]
num_summary <- do.call(cbind, lapply(df, summary)) %>% 
  t() %>% mutate(Interquatrile = `3rd Qu.` - `1st Qu.`)

Which would result in the following error

Error in UseMethod("mutate") : 
  no applicable method for 'mutate' applied to an object of class "c('matrix', 'array', 'double', 'numeric')"

Could you please explain what I did wrong and how to solve the problem without renaming the column names?


Solution

  • As mentioned in the other answer this has nothing to do with your column names and everything to do with the fact that cbind (as well as t) by default creates a matrix, not a data.frame.

    To create a table you don’t need to go the detour via a matrix (and as_tibble) at all — instead, use bind_cols or, in your case (omitting the t()), bind_rows:

    num_summary <- lapply(df, summary) %>%
      bind_rows() %>%
      mutate(Interquartile = `3rd Qu.` - `1st Qu.`)
    

    The code above preserves the class and attributes of the summary table. This isn’t harmful, but if you want to get rid of extraneous attributes and just want to retain the bare numeric values, you can apply as.vector (or c) to all columns to achieve that:

    num_summary <- lapply(df, summary) %>%
      bind_rows() %>%
      mutate(across(everything(), as.vector)) %>%
      mutate(Interquartile = `3rd Qu.` - `1st Qu.`)