# replacing values with special character

I have a summary like below

Name Sales
AS 71.5
DY 88.4
VH 44.6
MY 86.9
HU 42.3
TT 67.2
BG 0.0
SA 85.3

so if i want to replace if value is 0 then replace with "-"
do we have any solution

output should be

Name Sales
AS 71.5
DY 88.4
VH 44.6
MY 86.9
HU 42.3
TT 67.2
BG -
SA 85.3

This can be done easily but it convert all of your sales numbers into characters which will be very inconvenient if you need to do further calculations.

``````library(dplyr)

DF <- data.frame(Name = LETTERS[1:6], Sales = c(55.3, 78.2, 64.9, 0.0, 78.2, 32.1))
DF
#>   Name Sales
#> 1    A  55.3
#> 2    B  78.2
#> 3    C  64.9
#> 4    D   0.0
#> 5    E  78.2
#> 6    F  32.1
summary(DF)
#>  Name      Sales
#>  A:1   Min.   : 0.00
#>  B:1   1st Qu.:37.90
#>  C:1   Median :60.10
#>  D:1   Mean   :51.45
#>  E:1   3rd Qu.:74.88
#>  F:1   Max.   :78.20
DF <- DF %>% mutate(Sales = ifelse(Sales == 0, "-", Sales))
DF
#>   Name Sales
#> 1    A  55.3
#> 2    B  78.2
#> 3    C  64.9
#> 4    D     -
#> 5    E  78.2
#> 6    F  32.1
summary(DF)
#>  Name     Sales
#>  A:1   Length:6
#>  B:1   Class :character
#>  C:1   Mode  :character
#>  D:1
#>  E:1
#>  F:1
``````

Created on 2020-07-16 by the reprex package (v0.3.0)

yea further calculation also required

A data frame can only have one type of value in a column and I do not know of a way to format the value 0 to appear as "-" while remaining a number. Can you explain why you want to do this? There may be another solution.

I have already created a table summary

tab[,2] <- paste0(tab[,2],"%")
tab[,2] <- replace(tab[,2],tab[,2]<0,"-")

now want to change if summary have 0 or 0.0 then it should replace by "-"
i have used tab[,2] <- replace(tab[,2],tab[,2]<0,"-")
but its not giving the required output

If you run the following code, you will see that appending a % to the numbers converts them to characters, which you want to avoid. I suspect you are trying to handle an R data frame as if it were a spreadsheet. If you need to use the numbers in calculations, do not attempt to format their appearance, just leave them as numbers.

yea you are right, i have values with decimal points also,
if am doing this so the values with 18.0 or 7.0 also converting to "-"
do we have any other sollution then

I do not know of a way to format the value 0 to appear as "-" while remaining a number.
Why are you trying to change the appearance of the numbers?

Just retain numbers as numbers for all data manipulations and change them only for presentation purposes, e.g. in output tables.

tab[,2]<-ifelse(tab[,2]=="--","--",format(round(tab[,2],digits = 1),nsmall = 1))
tab[,2] <- paste0(tab[,2],"%")
tab[,2][tab[,2]==0] <- "-"

so this is part of my function and summary is coming out like below

Name Sales
AS 71.5%
DY 88.4%
VH 0.0%

so this 0.0% need to be replace with "-"
i this this will get resolved by gsub or sub i guess

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.