Search code examples
rmatrixtext-mining

Character-Matrix - how to operate by row?


I have a matrix of character data

charMatrix <- structure(c("Bolt", "Nut Plate", "Magnet", "", "Clevis", "welded", 
     "", "Receptacle"), .Dim = c(4L, 2L))

[,1]            [,2]        
[1,] "Bolt"      "Clevis"    
[2,] "Nut Plate" "welded"    
[3,] "Magnet"    ""          
[4,] ""          "Receptacle"

I want to paste the rows together and trim them to get the vector.

[1] "Bolt Clevis" "Nut Plate welded" "Magnet" "Receptacle"

I solved it this way, but I think there must be something much simpler, either in Base R or Tidyverse.

vec <- charMatrix %>% t() %>% 
  as_tibble(.name_repair = "universal") %>% 
  summarise_all(~ str_trim(paste(., collapse = " "))) %>% 
  unlist() %>% 
  as.character()

vec
[1] "Bolt Clevis"      "Nut Plate welded" "Magnet"           "Receptacle"     

Can you show me a more direct way to get this answer?


Solution

  • Since you have a matrix you can use rowwise apply and trimws to remove leading/trailing whitespaces

    trimws(apply(charMatrix, 1, paste, collapse = ' '))
    #[1] "BoltClevis"      "Nut Platewelded" "Magnet"          "Receptacle"     
    

    Or remove empty values and paste.

    apply(charMatrix, 1, function(x) paste(x[x!=''], collapse = ' '))