使用 gsub 删除不需要的字符:面临的问题

Using gsub for removing unwanted characters : facing issues

df$Claim_Value <- gsub("Rs.", "", df$`Total Amount Claimed`)

已选中 class(df$Total Amount Claimed):显示数字

gsub 是否适用于数字列?

这里 df$'Total Amount Claimed' 是一个金额为 Rs 的列。 例如:Rs.200000。试图删除卢比。从这个专栏。所以用了gsub。它的工作但显示金额以千而不是十万。

如何以十万为单位显示金额

structure(list(Approver = c("Amarjeet Singh", "Amit Barot", "Amit Barot", 
"Amit Barot", "Amit Barot", "Amit Barot"), `Assigned To` = c("SOLUTIONS.MOHALI@PALENGINEERS.IN", 
"CAMC2@SUDHIRGROUP.COM", "CAMC2@SUDHIRGROUP.COM", "CAMC2@SUDHIRGROUP.COM", 
"CAMC2@SUDHIRGROUP.COM", "CAMC2@SUDHIRGROUP.COM"), `Resolution Date` = structure(c(1609341652, 
1574165400, 1591818814, 1592327216, 1592397052, 1592496000), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), `Allow Submit Till Date` = structure(c(NA, 
1589414400, NA, NA, NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    `Amt App by CSS (Without Tax)` = c(NA, NA, NA, NA, NA, NA
    ), `ESN/Alternator No.` = c("AAG8045S126087", "84846607", 
    "22321621", "191014875", "25452001", "78939252"), `Auto Approved` = c("No", 
    "No", "No", "No", "No", "No"), BIS = c("N", "N", "N", "Y", 
    "Y", "N"), `Batch Amount` = c(NA, NA, NA, NA, NA, NA), `Batch Date` = c(NA, 
    NA, NA, NA, NA, NA), `Batch Number` = c(NA, NA, NA, NA, NA, 
    NA), `Category Of Service` = c("Maintenance or repair service", 
    "Maintenance or repair service", "Maintenance or repair service", 
    "Maintenance or repair service", "Maintenance or repair service", 
    "Maintenance or repair service"), `Claim Scope` = c("In Scope", 
    "In Scope", "In Scope", "In Scope", "In Scope", "In Scope"
    ), `Claim Type` = c("WARRANTY", "WARRANTY", "WARRANTY", "WARRANTY", 
    "WARRANTY", "WARRANTY"), `Customer Name` = c("SUDHIR POWER LIMITED", 
    "BHARGAV EARTH MOVERS", "WAGAD INFRA PROJECT PVT LTD", "SUDHIR POWER LIMITED", 
    "SUDHIR POWER LIMITED", "CORE MULTI SERVICE"), `Final Amount Approved...16` = c(NA, 
    NA, NA, NA, NA, NA), `Division Name` = c("Pal Engineers - Mohali", 
    "Sudhir (Ahmedabad) - Rajkot", "Sudhir Sales & Services Limited, Ahmedabad", 
    "Sudhir Sales & Services Limited, Ahmedabad", "Sudhir Sales & Services Limited, Ahmedabad", 
    "Sudhir Sales & Services Limited, Ahmedabad"), `Failure Type` = c("Warranty Failure", 
    "Warranty Failure", "Warranty Failure", "Warranty Failure", 
    "Warranty Failure", "Warranty Failure"), `GIEA Agreement Name` = c(NA, 
    NA, NA, NA, NA, NA), `Cummins Invoice Num` = c(NA, NA, NA, 
    NA, NA, NA), Agreement = c(NA, NA, NA, NA, NA, NA), `Problem Summary` = c("Electrical issue / PCC Controller  issues / Starter / alternator issue / Battery", 
    "Engine not starting / Tripping / Not stopping", "Maintenance / General Check", 
    "Engine not starting / Tripping / Not stopping", "Engine not starting / Tripping / Not stopping", 
    "Leakages  - Oil/ Fuel/ Coolant / Air"), `Resolution Summary` = c("After recharge battery tested and failed on load test replaced battery warranty", 
    "REPAIRED THE FUEL PUMP TAKEN TRAIL ALL PARAMETER LIMIT", 
    "Last service done by 23/12/2019 at 724 hours qt this time change air filter also.today service done at 974 hours.in between customer says top up oil 2.5 ltr then start the engine running ok now all parameters within limits.", 
    "attend site check & found starter loose connection then correct it & Suggested to customer requests load balances and require proper ventilation for dg set suction and discharge air .", 
    "ATTEND THE SITE OBSERVE ENGINE FOUND FAULT SHUTDOWN ERROR NEED TO VISIT OEM SIDE", 
    "ATTEND SITE CHECK & FOUND FUEL LEAKAGE FROM BLEIND PLUG THEN REMOVED IT & FITMENT GAIN & START ENGINE & FOUND ENGINE RUNNING WITHIN LIMIT.."
    ), `Claim Rejected` = c("Y", "Y", "Y", "Y", "Y", "Y"), `SR Number` = c("SR-PE-MO-2021-006884", 
    "SR-SU-RJ-1920-002793", "SR-SU-AH-2021-000683", "SR-SU-AH-2021-000857", 
    "SR-SU-AH-2021-000865", "SR-SU-AH-2021-000913"), `Service Type` = c(NA, 
    NA, NA, NA, NA, NA), `Sub Type` = c(NA, NA, NA, NA, NA, NA
    ), `Amount Claimed By Dealer` = c("Rs.5,721.00", "Rs.19,087.00", 
    "Rs.1,166.00", "Rs.836.00", "Rs.1,034.00", "Rs.2,057.00"), 
    `Processed By...29` = c("SOLUTIONS.MOHALI@PALENGINEERS.IN", 
    "CAMC2@SUDHIRGROUP.COM", "CAMC2@SUDHIRGROUP.COM", "CAMC2@SUDHIRGROUP.COM", 
    "CAMC2@SUDHIRGROUP.COM", "CAMC2@SUDHIRGROUP.COM"), `Claim #` = c("1-5W4ZZVR", 
    "1-5PWNEAT", "1-5QQ4Z4J", "1-5QWC86P", "1-5QXPYU1", "1-5QXU7VN"
    ), `Claim Category` = c("STANDARD", "STANDARD", "STANDARD", 
    "STANDARD", "STANDARD", "STANDARD"), `Claim Creation Date` = structure(c(1609844392, 
    1588360803, 1591890038, 1592481430, 1592577627, 1592582659
    ), class = c("POSIXct", "POSIXt"), tzone = "UTC"), `Created By` = c("1-5LS00O1", 
    "1-2CD07UT", "1-2CD07UT", "1-2CD07UT", "1-2CD07UT", "1-2CD07UT"
    ), `Currency Code` = c("INR", "INR", "INR", "INR", "INR", 
    "INR"), Partner = c(NA, NA, NA, NA, NA, NA), `Final Amount Approved...36` = c("Rs.0.00", 
    "Rs.0.00", "Rs.0.00", "Rs.0.00", "Rs.0.00", "Rs.0.00"), `Fund Req Category` = c(NA, 
    NA, NA, NA, NA, NA), Comments = c(NA, NA, NA, NA, NA, NA), 
    `Claim Name` = c("CLM-PE-MO-2021-002442", "CLM-SU-RJ-2021-000055", 
    "CLM-SU-AH-2021-000527", "CLM-SU-AH-2021-000627", "CLM-SU-AH-2021-000641", 
    "CLM-SU-AH-2021-000643"), `Organization Name` = c("Pal Engineers, Jammu", 
    "Sudhir Sales & Services Limited, Ahmedabad, AHMEDABAD", 
    "Sudhir Sales & Services Limited, Ahmedabad, AHMEDABAD", 
    "Sudhir Sales & Services Limited, Ahmedabad, AHMEDABAD", 
    "Sudhir Sales & Services Limited, Ahmedabad, AHMEDABAD", 
    "Sudhir Sales & Services Limited, Ahmedabad, AHMEDABAD"), 
    Period = c(NA, NA, NA, NA, NA, NA), `Pre-Approval #` = c(NA, 
    NA, NA, NA, NA, NA), `Processed By...43` = c("SOLUTIONS.MOHALI@PALENGINEERS.IN", 
    "CAMC2@SUDHIRGROUP.COM", "CAMC2@SUDHIRGROUP.COM", "CAMC2@SUDHIRGROUP.COM", 
    "CAMC2@SUDHIRGROUP.COM", "CAMC2@SUDHIRGROUP.COM"), `Program Account Name` = c(NA, 
    NA, NA, NA, NA, NA), `Program Name` = c(NA, NA, NA, NA, NA, 
    NA), `Promotion Name` = c("BTRY_CHANDIGARH", "CIL_20000", 
    "CIC_5000", "Warranty_BIS_RECON", "Warranty_BIS_RECON", "CIC_5000"
    ), Description = c(NA, NA, NA, NA, NA, NA), Status = c("Pending", 
    "Pending", "Pending", "Pending", "Pending", "Pending"), `Final Approval Date` = c(NA, 
    NA, NA, NA, NA, NA), `Submitted By` = c("SOLUTIONS.MOHALI@PALENGINEERS.IN", 
    "WARRANTY.AHD@SUDHIRGROUP.COM", "WARRANTY.AHD@SUDHIRGROUP.COM", 
    "WARRANTY.AHD@SUDHIRGROUP.COM", "WARRANTY.AHD@SUDHIRGROUP.COM", 
    "WARRANTY.AHD@SUDHIRGROUP.COM"), `Total Amount Approved` = c(0, 
    0, 0, 0, 0, 0), `Total Amount Claimed` = c("Rs.5,721.00", 
    "Rs.19,087.00", "Rs.1,166.00", "Rs.836.00", "Rs.1,034.00", 
    "Rs.2,057.00"), `Total Participation Amount` = c(NA, NA, 
    NA, NA, NA, NA), Updated = structure(c(1610113437, 1589227258, 
    1591896091, 1592491326, 1592645576, 1592839702), class = c("POSIXct", 
    "POSIXt"), tzone = "UTC"), `Updated By` = c("1-4YO9LU", "1-2QTU4R", 
    "1-SDR5", "1-SDRU", "1-SDRU", "1-SDR5"), `Resolved By FSL` = c("Y", 
    "N", "Y", "Y", "N", "N"), `Parts Warranty Claim` = c(NA, 
    NA, NA, NA, NA, NA), `Inbox Last Updated` = structure(c(1610113437, 
    1589227258, 1591896091, 1592491326, 1592645576, 1592839702
    ), class = c("POSIXct", "POSIXt"), tzone = "UTC"), `Claim Submitted Date` = structure(c(1609939043, 
    NA, NA, NA, NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    `Claim Rejection Reason` = c("Incorrect/Missing Commercial Bills", 
    "Incorrect/Missing Technical Documents", "HCS or KAM Approval Required", 
    "Incorrect/Missing Technical Documents", "Incorrect/Missing Technical Documents", 
    "Incorrect/Missing Technical Documents"), `Claim Categorization Reason` = c(NA, 
    NA, NA, NA, NA, NA), Aging = c(2.4278125, 244.16599537037, 
    213.276724537037, 206.387430555556, 204.60212962963, 202.355300925926
    ), AgeGroup = structure(c(2L, 6L, 6L, 6L, 6L, 6L), .Label = c("0-1 Days", 
    "2-4 Days", "5-7 Days", "8-15 Days", "16-30 Days", ">30 Days"
    ), class = "factor"), Zones = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), Approver.y = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), Claim_Value = c(NA, 
    NA, NA, 836L, NA, NA)), row.names = c(NA, 6L), class = "data.frame")

以下应该可以正常工作。

as.numeric(gsub("Rs.", "", "Rs 2000"))

提供的df$`Total Amount Claimed` 列是字符类型而不是因子类型。 要以十万而不是指数格式显示,请使用选项

options("scipen"=100, "digits"=4)

您可以按以下方式使用 gsub 将值转换为数字:

df$`Total Amount Claimed`
#[1] "Rs.5,721.00" "Rs.19,087.00" "Rs.1,166.00" "Rs.836.00"  "Rs.1,034.00"  "Rs.2,057.00" 

df$Claim_Value <- as.numeric(gsub('Rs\.|,', '', df$`Total Amount Claimed`))
df$Claim_Value
#[1]  5721 19087  1166   836  1034  2057