Alex Bădoi
Alex Bădoi

Reputation: 830

how to delete the first n characters of rownames in a data frame?

I have a data-frame which looks like this:

                    date_tmp  efficiency        smb      rmrf      hml   rmw      cma    Returns
2010-11-04.Stock1 2010-11-04  0.4469618 0.87430339 0.7337814 0.00000000   0 0.60769928 1.245834
2010-11-04.Stock2 2010-11-04  0.6608003 0.65057967 2.2088113 0.00000000   0 0.10672836 1.659817
2010-11-04.Stock3 2010-11-04  0.3878181 0.88259984 0.2530008 0.04665325   0 1.38739047 1.388948
2010-11-04.Stock4 2010-11-04  0.4781154 0.93226537 0.0000000 0.16319179   0 1.43025290 1.331511
2010-11-04.Stock5 2010-11-04  0.4809276 0.56507215 1.9185010 0.00000000   0 0.09270075 1.441663
2010-11-04.Stock6 2010-11-04  0.4619094 0.06452728 1.8513120 0.00000000   0 0.40841138 1.251019

The first column shows the row names.

What i need is to remove the date part on all rows eg( "Stock1" instead of "2010-11-04.Stock1"

I basically need the reverse of this function which keeps the first n characters instead of deleting them.

x <- "some text in a string"

substrRight <- function(x, n){
  substr(x, 1, n)
}

substrRight(x, 4)
[1] "some"

Upvotes: 3

Views: 4349

Answers (2)

Roman Luštrik
Roman Luštrik

Reputation: 70643

There are several ways of tackling this. Here are two. The elaborate regex is there for readability, assuming the date is consistently used.

> x <- "2010-11-04.Stock1"
> 
> strsplit(x, split = "\\.")[[1]][2]
[1] "Stock1"
> gsub("\\d{4}-\\d{2}-\\d{2}\\.(\\w+)", replacement = "\\1", x = x)
[1] "Stock1"

To use strsplit you'll need to use sapply and do.call which may not be as pretty as gsub.

Upvotes: 0

comendeiro
comendeiro

Reputation: 836

You might want to consider using Regular expressions if the date part is what you want to remove, for instance

foo = function(x){
    return(gsub(".+\\.","",x))
}

If you do foo("2010-11-04.Stock1") will return you "Stock1"

Upvotes: 1

Related Questions