albert
albert

Reputation: 325

remove some characters in a data.frame in R

I need to remove some characters in a data,frame and I dont know:

suppose we have the data.frame

       X1            X2
1  2:2.627488   3:3.507524  ...
2  2:4.734847   3:8.465927  ...
3  2:7.185827   3:12.939696 ...
4  2:6.923039   3:20.863585 ...
5  2:7.898322   3:19.106577 ...
.      .              .
.      .              .
.      .              .

how to remove "2:" ann "3:" in R?

ie the data.frame looks like this:

      X1            X2
1   2.627488     3.507524  ...
2   4.734847     8.465927  ...
3   7.185827     12.939696 ...
4   6.923039     20.863585 ...
5   7.898322     19.106577 ...
.      .              .
.      .              .
.      .              .

Help me !

Upvotes: 0

Views: 1053

Answers (2)

Sandipan Dey
Sandipan Dey

Reputation: 23101

You can try this also:

as.data.frame(gsub('[0-9]:', '', as.matrix(df)))

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520888

Assuming that the pattern \d+: only occurs once, at the start of each entry in the data frame, then you can use gsub() to remove the unwanted prefix:

df <- apply(df, 2, function(x) { x <- gsub("\\d+:", "", x) })

Upvotes: 2

Related Questions