Question

I have a matrix that looks lke this:

wun2                               308.0      111.0       72.0       64.0
wupA                              8177.0     3933.0     2235.0     2057.0
wus                                  5.0        0.0        9.0        5.0
x16                                581.2      154.3      297.1      384.2
xmas-1                              26.5       58.0       26.5       31.0
xmas-2                             201.5      138.2      136.5      203.0
y                                   18.0        1.0       13.0        8.0
yar                                  5.0        0.0        0.0        2.0
yata                              1283.0      679.0      735.0      784.0
yellow-b                           129.0       82.0      103.0       96.0
yellow-c                           817.3      415.0      832.0      788.0
zormin                            3447.0     2005.0     1999.0     2300.0
zpg                                  1.5        0.0        0.0        0.0
zwilch                               1.0        0.0        8.0       14.0
zye                                 83.0       64.0       17.0       30.8

I want to round that matrix, but I want all the numbers whose last decimal is 5 to be rounded up to the next integer. Therefore, I wanted to apply a the ceiling function to all cells that end in .(somenumber+)5 before doing a general round().

I don't know how to specify that as a shell expression to glob2rx so it will make the adequate regular expression, but I inferred it had to be something like this:

 grx <-  "^\\d+\\.\\d+5$"

However, this doesn't seem to be getting any of the cells when I do grep(grx, x=matrix.1, value=TRUE).

What could be wrong?

Thanks! Carmen

Was it helpful?

Solution

Here's a way with base R:

d <- read.table(text="
  wun2                               308.0      111.0       72.0       64.0
  wupA                              8177.0     3933.0     2235.0     2057.0
  wus                                  5.0        0.0        9.0        5.0
  x16                                581.2      154.3      297.15      384.2
  xmas-1                              26.5       58.0       26.5       31.0
  xmas-2                             201.5     138.220      136.5      203.0
  y                                   18.0        1.0       13.0        8.0
  yar                                  5.0        0.0        0.0        2.0
  yata                              1283.0      679.0      735.0      784.0
  yellow-b                           129.0       82.0      103.0       96.0
  yellow-c                           817.345    415.0      832.0      788.0
  zormin                            3447.0     2005.0     1999.0     2300.0
  zpg                                  1.0        0.0        0.0        0.0
  zwilch                               1.0        0.0        8.0       14.0
  zye                                 83.0       64.0       17.0       30.865")

subbed <- lapply(d[-1], 
    function(col) ifelse(grepl('\\d+\\.\\d*5$', col), ceiling(col), col))
result <- data.frame(d[1], subbed)


#          V1     V2      V3   V4     V5
# 1      wun2  308.0  111.00   72   64.0
# 2      wupA 8177.0 3933.00 2235 2057.0
# 3       wus    5.0    0.00    9    5.0
# 4       x16  581.2  154.30  298  384.2
# 5    xmas-1   27.0   58.00   27   31.0
# 6    xmas-2  202.0  138.22  137  203.0
# 7         y   18.0    1.00   13    8.0
# 8       yar    5.0    0.00    0    2.0
# 9      yata 1283.0  679.00  735  784.0
# 10 yellow-b  129.0   82.00  103   96.0
# 11 yellow-c  818.0  415.00  832  788.0
# 12   zormin 3447.0 2005.00 1999 2300.0
# 13      zpg    1.0    0.00    0    0.0
# 14   zwilch    1.0    0.00    8   14.0
# 15      zye   83.0   64.00   17   31.0

OTHER TIPS

Does something like this help?

library(stringr)

a <- read.table("test.txt")

apply(a[, 2:ncol(a)], 2, function(x) {
    w <-str_detect(x, "\\.[0-9]{2,}") & str_sub(x, start=-1) == 5
    x[w] <- ceiling(x[w])
    round(x,2)})

          V2      V3     V4     V5
 ## [1,]  308.0  111.00   72.0   64.0
 ## [2,] 8177.0 3933.00 2235.0 2057.0
 ## [3,]    5.0    0.00    9.0    5.0
 ## [4,]  581.2  154.30  298.0  384.2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top