I have a numerical column with NAs, negative, positive, and 0 values. What I want is the following:
- If the value is positive, I want to add "+" in front
- If it is Negative, 0, or NA, leave it as it is:
Data:
df <- data.frame (a = c(12,-34,NA,-23,5,0,NA))
Expected outcome:
a
1 +12
2 -34
3 NA
4 -23
5 +5
6 0
7 NA
>Solution :
Another possible solution, based on formatC:
gsub("NA", NA, formatC(df$a, flag = "+0", zero.print = T))
#> [1] "+12" "-34" NA "-23" "+5" "0" NA