regex - glob2rx in R to get all cells whose last decimal is 5? -
i have matrix 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 round matrix, want numbers last decimal 5 rounded next integer. therefore, wanted apply ceiling function cells end in .(somenumber+)5 before doing general round().
i don't know how specify shell expression glob2rx make adequate regular expression, inferred had this:
grx <- "^\\d+\\.\\d+5$"
however, doesn't seem getting of cells when grep(grx, x=matrix.1, value=true)
.
what wrong?
thanks! carmen
here's way 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
Comments
Post a Comment