Christoph Christoph - 4 months ago 9
R Question

How can I transform a matrix of characters with a few lines of code to a different matrix?

I have the following matric (I do not stick to a matrix type - it could also be a data.frame, ...):

df <- structure(c("Jan-01", "Dec-31", "00-00", "24-00", "Jan-01", "Jun-30",
"12-00", "18-00", "Jul-06", "Dec-31", "09-00", "19-00", "Jul-06",
"Dec-31", "09-00", "19-00"), .Dim = c(4L, 4L), .Dimnames = list(
NULL, c("V1", "V2", "V3", "V4")))

# V1 V2 V3 V4
# [1,] "Jan-01" "Jan-01" "Jul-06" "Jul-06"
# [2,] "Dec-31" "Jun-30" "Dec-31" "Dec-31"
# [3,] "00-00" "12-00" "09-00" "09-00"
# [4,] "24-00" "18-00" "19-00" "19-00"


How can I make it look like

V1 V2 V3 V4
[1,] "Jan" "Jan" "Jul" "Jul"
[2,] "01" "01" "06" "06"
[3,] "Dec" "Jun" "Dec" "Dec"
[4,] "31" "30" "31" "31"
[5,] "00" "12" "09" "09"
[6,] "00" "00" "00" "00"
[7,] "24" "18" "19" "19"
[8,] "00" "00" "00" "00"


that means: split each cell by "-" and transform it to vector with 2 rows.

I tried with the methods from here but the results were far away from good, e.g.
t(str_split_fixed(new_standard, "\\-", 8))

Answer

Here is one solution:

library(stringr)
matrix(t(str_split_fixed(df, "-", 2)), ncol = 4)
#      [,1]  [,2]  [,3]  [,4] 
# [1,] "Jan" "Jan" "Jul" "Jul"
# [2,] "01"  "01"  "06"  "06" 
# [3,] "Dec" "Jun" "Dec" "Dec"
# [4,] "31"  "30"  "31"  "31" 
# [5,] "00"  "12"  "09"  "09" 
# [6,] "00"  "00"  "00"  "00" 
# [7,] "24"  "18"  "19"  "19" 
# [8,] "00"  "00"  "00"  "00"