0

enter image description here

The added image is created from the following code:

  B <- addmargins(table(data$range, data$symbols))
   B[] <- sprintf('%s (%s)', as.matrix(B), paste0(round(prop.table(as.matrix(B), 1), 2) * 200, '%'))
   knitr::kable(B)%>%
   kable_styling(bootstrap_options= c("striped", "hover"), font_size=16, position="center")
   ```

The last column is called SUM, which you can see. I want to remove the % sign from the last column. To achieve this, I tried using gsub like so

res <- gsub("%","", B[ , ncol(B[])])
knitr::kable(res)%>%
kable_styling(bootstrap_options= c("striped", "hover"), font_size=16, position="center")
```

It does remove the % signs, but it also removes all the other columns, and only shows the last column. How can I make it keep the other columns?

Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29
States
  • 141
  • 11
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 25 '23 at 13:30
  • 1
    The result of your `gsub` as above is the column after "%" removed. If you only want to revise that column and keep the result of your data.frame, try instead something like `B[,ncol(B)] <-gsub("%", "", B[,ncol(B)])`? – Ben Apr 25 '23 at 18:31
  • Thanks @Ben , this is exactly what I wanted. If you can make it so that I can mark your answer as the solution, I'll do that. – States Apr 27 '23 at 07:23
  • By the way. If I wanted to remove the entire text that says 100%, instead of just the "%" sign, how would I do that? I tried doing: ´B[ , ncol(B)] <- gsub("(100)%"," ", B[ , ncol(B[])])´. But this makes it look like ´()´ – States Apr 27 '23 at 07:33
  • @States Glad this helped - also, see my edited answer on how you can avoid needing to remove "%" as well. – Ben Apr 27 '23 at 16:00

1 Answers1

1

First, you can avoid the need to remove "%" by just adding percentages to columns that do not include the Sum column (referring to B[,-ncol(B)] instead of B or B[] in the sprintf statement will add percentages to all columns except the last one).

In addressing the posted question, using made up data based on the example image above:

B <- data.frame(
  O = c("14 (54%)", "20 (64%)"),
  A = c("9 (34%)", "5 (16%)"),
  AB = c("0 (0%)", "1 (4%)"),
  B = c("3 (12%)", "5 (16%)"),
  Sum = c("26 (100%)", "31 (100%)")
)

You can use the following to just remove "%" (original question):

B[,ncol(B)] <- sub("%", "", B[,ncol(B)])
B

         O       A     AB       B      Sum
1 14 (54%) 9 (34%) 0 (0%) 3 (12%) 26 (100)
2 20 (64%) 5 (16%) 1 (4%) 5 (16%) 31 (100)

To remove the open parenthesis "(" and everything after it (follow up comment) you can do:

B[,ncol(B)] <- sub(" \\(.*", "", B[,ncol(B)])
B

         O       A     AB       B Sum
1 14 (54%) 9 (34%) 0 (0%) 3 (12%)  26
2 20 (64%) 5 (16%) 1 (4%) 5 (16%)  31

To remove both the parentheses and all text contained between them, you can do:

B[,ncol(B)] <- sub(" \\(.*)", "", B[,ncol(B)])
B

Note: If you have multiple parentheses, you can use gsub and a different pattern.

Ben
  • 28,684
  • 5
  • 23
  • 45