1

Input:

df = data.frame(1:3)
rownames(df) <- c("a.bzz.-11", "bb.c.44", "cdd.d.3")

Expected output:

            X1.5    col1    col2    col3
a.bzz.-11   1      a        bzzz    -11
bb.c.44      2     bb       c       44
cdd.d.3     3      cdd      d       3

There are few interesting discussions here but not working in my case. Thanks in advance.

Community
  • 1
  • 1
Rashedul Islam
  • 879
  • 1
  • 10
  • 23

1 Answers1

4

We can use read.table to read the row names into the three column by using the delimiter as ..

cbind(df, read.table(text=row.names(df), sep=".", 
          header=FALSE, col.names = paste0("col", 1:3), stringsAsFactors=FALSE))
#          X1.3 col1 col2 col3
#a.bzz.-11    1    a  bzz  -11
#bb.c.44      2   bb    c   44
#cdd.d.3      3  cdd    d    3
akrun
  • 874,273
  • 37
  • 540
  • 662