Skip to content
Prev 279611 / 398506 Next

removing specified length of text after a period in dataframe of char's

Hi Sarah,

apologies for the excess. A smaller example:

f<-structure(list(c("GDP per capita (LCU)", "Ratio to EZ GDP Per Cap"
), `2005` = c(32128, 0.1), `2009` = c(52163, 0.1), `2010` = c(63100,
0.1), `2011` = c(72461, 0.1), `2012` = c(81313, 0.1)), .Names = c("",
"2005", "2009", "2010", "2011", "2012"), row.names = 3:4, class = c("cast_df",
"data.frame"))

nam2<-
structure(list(var1 = c("GDP per capita (LCU)", "Ratio to EZ GDP Per Cap"
), digi = c(0, 1)), .Names = c("var1", "digi"), row.names = c("98",
"110"), class = "data.frame")

I'm trying to place a thousand separator in the numbers in the table f:
2005    2009    2010    2011    2012
3    GDP per capita (LCU) 32128.0 52163.0 63100.0 72461.0 81313.0
4 Ratio to EZ GDP Per Cap     0.1     0.1     0.1     0.1     0.1

and also have precision given by variable digi:
var1 digi
98     GDP per capita (LCU)    0
110 Ratio to EZ GDP Per Cap    1

format
  hi<-format(f,big.mark=",",scientific=F)
gives me the comma, but now I'm not sure how to get the precision.

Your answer seems to be doing what I want, although when I changed the
testdata slightly
values digits
1 10,000.0      0
2      5.3      1
3      1.1      2
1          2          3
 "10,000." "     5.3" "     1.1"
The decimal appears to be left behind in 10,000.

Unfortunately your approach is a bit too advanced for me, so I can't
adapt it. Perhaps you could recommend somewhere where I could read up
on what the caret and other symbols mean in your paste call?

thanks for your help!

Aidan
On Wed, Dec 7, 2011 at 12:05 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote: