将“%”添加到 R 中列中的值

Adding "%" to values in a column in R

我有以下数据框

  `Mentor Name` Completed `In Progress` `Not Started` `Percent Completed`
  <chr>             <int>         <int>         <int>               <dbl>
1 Betsy                 6            11            18                  17
2 Feliciano             8            11            16                  23
3 Gloria                5            15            16                  14
4 Linda                 5            12            19                  14
5 Shannon               9             5            22                  25
6 Shaune                4            16            15                  11
7 Sheryl                5            12            19                  14
structure(list(`Mentor Name` = c("Betsy", "Feliciano", "Gloria", 
"Linda", "Shannon", "Shaune", "Sheryl"), Completed = c(6L, 8L, 
5L, 5L, 9L, 4L, 5L), `In Progress` = c(11L, 11L, 15L, 12L, 5L, 
16L, 12L), `Not Started` = c(18L, 16L, 16L, 19L, 22L, 15L, 19L
), `Percent Completed` = c(17, 23, 14, 14, 25, 11, 14)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -7L), groups = structure(list(
    `Mentor Name` = c("Betsy", "Feliciano", "Gloria", "Linda", 
    "Shannon", "Shaune", "Sheryl"), .rows = structure(list(1L, 
        2L, 3L, 4L, 5L, 6L, 7L), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -7L), .drop = TRUE))

我想在“完成百分比”列中添加一个“%”。

我们可以使用 pastestr_c

library(dplyr)
library(stringr)
df1 %>% 
   ungroup %>%
   mutate(`Percent Completed` = str_c(`Percent Completed`, '%'))

-输出

# A tibble: 7 × 5
  `Mentor Name` Completed `In Progress` `Not Started` `Percent Completed`
  <chr>             <int>         <int>         <int> <chr>              
1 Betsy                 6            11            18 17%                
2 Feliciano             8            11            16 23%                
3 Gloria                5            15            16 14%                
4 Linda                 5            12            19 14%                
5 Shannon               9             5            22 25%                
6 Shaune                4            16            15 11%                
7 Sheryl                5            12            19 14%       

base R中:

df$`Percent Completed` <- paste(df$`Percent Completed`, "%", sep = "")

结果:

df
# A tibble: 7 x 5
# Groups:   Mentor Name [7]
  `Mentor Name` Completed `In Progress` `Not Started` `Percent Completed`
  <chr>             <int>         <int>         <int> <chr>              
1 Betsy                 6            11            18 17%                
2 Feliciano             8            11            16 23%                
3 Gloria                5            15            16 14%                
4 Linda                 5            12            19 14%                
5 Shannon               9             5            22 25%                
6 Shaune                4            16            15 11%                
7 Sheryl                5            12            19 14%