Reputation: 325
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
Reputation: 23101
You can try this also:
as.data.frame(gsub('[0-9]:', '', as.matrix(df)))
Upvotes: 0
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