I have a column in my df as given and I want to convert values which are in cents into dollars and those which are in dollars I want them as it is.
            Asked
            
        
        
            Active
            
        
            Viewed 317 times
        
    0
            
            
        - 
                    1Please give a [mre] in your question! Use `dput()` or a definition of your dataframe to show your data. IMHO it woulb be better you have two columns: one for the numeric value and one for the currency unit. – jogo Oct 18 '19 at 06:33
- 
                    jogo I was having my data in this format only and other columns contain character values like names. – xyz Oct 18 '19 at 06:39
- 
                    Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jogo Oct 18 '19 at 06:41
1 Answers
2
            There are more elegant ways to do this. This is my approach for now:
df <- data.frame(
    money = c("69¢", "40.6¢", "91.3¢", "50¢", "4¢", "$1.17", "$1", "$1.30")) 
dollar = as.character(df$money)
cents <- as.numeric(unlist(strsplit(dollar, "¢"))) 
dollar[!is.na(cents)]<-paste0("$", round((cents/100), 2))
df$money = dollar
df
> df
  money
1 $0.69
2 $0.41
3 $0.91
4  $0.5
5 $0.04
6 $1.17
7    $1
8 $1.30
 
    
    
        Zhiqiang Wang
        
- 6,206
- 2
- 13
- 27
- 
                    
- 
                    Yes, that's why you need to provide minimal reproducible example. Ok, I will revise it. – Zhiqiang Wang Oct 18 '19 at 07:08
- 
                    

