R change $xxx.xx to xxx.xx for both positive and negative numbers but don't round

后端 未结 4 1836
我寻月下人不归
我寻月下人不归 2021-01-22 20:36

I have a df where columns 2 and beyond are dollar amounts such as $1004.23, ($1482.40), $2423.94 etc. Similar to the example below:

> df
  id   desc    price
         


        
4条回答
  •  长发绾君心
    2021-01-22 21:29

    for(i in 1:nrow(df)){
        df[i,3] <- as.character(sub(")", "", sub("(", "-", as.character(df[i,3]), fixed=TRUE), fixed=TRUE))
        df[i,3] <- as.numeric(gsub('[$,]', '', df[i,3]))
    }
    

提交回复
热议问题