Home > Blockchain >  Subset R data.frame by column name with hyphen "-" in name (using dplyr)
Subset R data.frame by column name with hyphen "-" in name (using dplyr)

Time:01-14

I have a model output which is a matrix with 1 column and many rows. Matrix column name is dependent on input pair of conditions and looks like "St1-St2". Column values might be 0 or 1 or -1.

I would like to filter values != 0.

# matrix example
mat1.data <- c(1,0,0,0,1,-1,-1,0,1)
m_rownames <- c("g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8", "g9")
mat1 <- matrix(mat1.data, nrow=9,ncol=1,byrow=TRUE)
colnames(mat1) <- "St1-St2"
rownames(mat1) <- m_rownames

mat1

# get column name
m_column <- colnames(mat1) 

# try to filter using rlang's injection 
mat2 <- data.frame(mat1) %>% 
  dplyr::filter(!!as.symbol(m_column) != 0) %>% 
  rownames_to_column()

How to deal with hyphen in column name?

After evaluating the code I get error:

#> Error in `dplyr::filter()`:
#> ! Problem while computing `..1 = `St1-St2`type here != 0`.
#> Caused by error in `mask$eval_all_filter()`:
#> ! object 'St1-St2' not found
#> Run `rlang::last_error()` to see where the error occurred.

CodePudding user response:

Use as.data.frame instead of data.frame as data.frame calls make.unique checks on the column name and as the column name got some - characters, it is changed to .. With as.data.frame, it remains as such (or else have to call check.names = FALSE

library(dplyr)
as.data.frame(mat1) %>% 
  dplyr::filter(!!as.symbol(m_column) != 0) %>% 
  rownames_to_column()

-output

   rowname St1-St2
1      g1       1
2      g5       1
3      g6      -1
4      g7      -1
5      g9       1
  • Related