使用R编程进行数据清理

时间:2018-07-09 18:36:47

标签: r

我在数据框中有“年龄”列。年龄被提到为22,7 18,5 25,4 我需要将替换为小数。如何在R中做到这一点?

from __future__ import print_function
from mailmerge import MailMerge
from datetime import date
from docx import Document
from docx.opc.constants import CONTENT_TYPE as CT
import csv
import sys
import os
import numpy as np
import pandas as pd

# . . .

for i in range(0, numTemplates):
   theTemplateName = templateNameCol[i]
   theTemplateFileLocation = templateFileLocationCol[i]
   template = theTemplateFileLocation
   document = MailMerge(template)
   print(document.get_merge_fields())

   theOffice = officeCol[i]
   theAddress = addressCol[i]
   theSuite = suiteCol[i]
   theCity = cityCol[i]
   theState = stateCol[i]
   theZip = zipCol[i]
   thePhoneNum = phoneNumCol[i]
   theFaxNum = faxNumCol[i]

   document.merge(
       Address = theAddress 
   )

   document.write(r'\Users\me\mailmergeproject\test-output' + str(i) + r'.docx')
   #do conversion here

2 个答案:

答案 0 :(得分:2)

我们可以使用sub,替换为.并转换为numeric

as.numeric(sub(",", ".", age))
#[1] 22.7 18.5 25.4

或与chartr

as.numeric(chartr(",", ".", age))

数据

age <- c("22,7", "18,5", "25,4")

答案 1 :(得分:2)

用逗号替换逗号的一种方法是将scan与参数dec = ","一起使用。

scan(text = "22,7 18,5 25,4", dec = ",")
#Read 3 items
#[1] 22.7 18.5 25.4


y <- c("22,7", "18,5", "25,4")

scan(textConnection(y), dec = ",")
#Read 3 items
#[1] 22.7 18.5 25.4