Question

I have a problem where I would appreciate any ideas on how to do it with R. The problem is this: I have a latex-table stored. The numbers in the table are all equipped with three digits after the decimal point. I want to cut these digits off, leaving the others in the table. (Think of the numbers representing estimation results, but with dimension "dollar". Then, a value of 145.553 does not make much sense, and 145 is enough). The person who created these tables did not think too much about this, so here I go trying to avoid going through the table by hand. :)

So far, I only found different solutions for how to extract numbers from strings, not how to change them so that the string itself is unaltered otherwise.

Example:

 strings <- c(
 "a.name      & $-436.735  $   & $-710.832$   \\\\", 
 "std(a.name) & $(1403.604)$   & $(1274.283)$ \\\\", 
 )

The solution should return

 strings <- c(
 "a.name      & $-436  $   & $-710$   \\\\", 
 "std(a.name) & $(1403)$   & $(1274)$ \\\\", 
 )

and, of course, if it was possible to do the rounding correctly, then it would be even better. But this is not of upmost importance.

I tried using gsub with \\.... to identify the strings that contain a period followed by three other numbers, but this also gives me the variable names, a.name in my example.

Does anyone have an idea how I could accomplish what I would like to do?

Thanks in advance!

Was it helpful?

Solution

This uses base R's gregexpr, regmatches, and regmatches<- to round any number with a decimal part.

It will work correctly even for numbers like .789 (i.e. with no digits before the decimal point) and -0.4 (which should round to a number without a minus sign). The one situation where it might perform less than ideally is that it will not remove the trailing decimal from a number like 10. .

string <- c("a.name      & $-436.735  $   & $-710.832$   \\\\", 
            "std(a.name) & $(1403.604)$   & $(1274.283)$ \\\\")

f <- function(x) {
    pat <- "(-)?[[:digit:]]+\\.[[:digit:]]*"
    m <- gregexpr(pat, x)
    regmatches(x,m) <- lapply(regmatches(x,m), function(X) round(as.numeric(X)))
    x
}

f(string)
# [1] "a.name      & $-437  $   & $-711$   \\\\"
# [2] "std(a.name) & $(1404)$   & $(1274)$ \\\\"

OTHER TIPS

gsub(strings, pattern ="\\.[[:digit:]]{3}", replacement = "")
#[1] "a.name      & $-436  $   & $-710$   \\\\" "std(a.name) & $(1403)$   & $(1274)$ \\\\"

To get the rounding, I'd do something along these lines but the brackets make it a little ugly -

sapply(
  strsplit(
    strings,
    "\\$|\\$\\(|\\)\\$"
  ),
  function(x) 
    paste(
      x[1],'$',
      ifelse(as.numeric(x[2]) <0, round(as.numeric(x[2]),0),paste0("(",round(as.numeric(x[2]),0),")")),'$',
      x[3],'$',
      ifelse(as.numeric(x[4]) <0, round(as.numeric(x[4]),0),paste0("(",round(as.numeric(x[4]),0),")")),'$',
      x[5]
      )
)
#[1] "a.name      &  $ -437 $    &  $ -711 $    \\\\"   "std(a.name) &  $ (1404) $    &  $ (1274) $  \\\\"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top