Home > Back-end >  Adding "%" to values in a column in R
Adding "%" to values in a column in R

Time:10-13

I have the following dataframe

  `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))

I want to add a "%" to the "Percent Completed" column.

CodePudding user response:

We may either use paste or str_c

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

-output

# 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%       

CodePudding user response:

In base R:

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

Result:

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%  
  • Related