Reputation: 1907
I have a large character matrix:
Chromosome Start End ...
1 1 5000 ...
2 1 4000 ...
... ... ...
I would like to subtract 1 from each entry of the second column. The first idea that comes to mind is to separate each column, convert the second as numeric, perform the subtraction and use cbind to patch everything together. However, since I have over 20 columns, I want something more efficient.
I would appreciate your input.
Thank you!
Upvotes: 1
Views: 213
Reputation: 263311
Why not just (now assuming character matrix but would also work w/ datafreme.)
dat[ ,2 ] <- as.numeric(dat[, 2 ])-1
Upvotes: 2
Reputation: 92282
I would recommend gsubfn
package as it is designed especially for cases like that, in particular:
library(gsubfn)
dat[, 2] <- gsubfn("\\d", function(x) as.numeric(x) - 1, dat[, 2])
dat
## Chromosome Start End
## [1,] "1" "0" "5000"
## [2,] "2" "0" "4000"
Upvotes: 0