如何使用R从CSV创建XPT文件

时间:2017-12-07 07:47:11

标签: r csv

我们如何读取CSV文件并将数据输入到R中的write。xport()函数?

这里主要与我读取的csv数据转换为输入write.xport()功能相混淆。我刚开始学习R。

谢谢!

1 个答案:

答案 0 :(得分:2)

以下是使用Baltimore City Employee Salary数据读取CSV文件的示例。

#
# City of Baltimore 2016 employee salary data
#

theFile <- "https://data.baltimorecity.gov/api/views/65ac-s4v5/rows.csv?accessType=DOWNLOAD"

download.file(theFile,
              "./data/baltimoreEmployeeSalaries2016.csv",
              method="wininet", # for Windows, use "curl" for OS X / Linux
              mode="w")

salaries <- read.csv("./data/baltimoreEmployeeSalaries2016.csv",header=TRUE,stringsAsFactors=FALSE)
head(salaries)

...和输出。

> head(salaries)
                   Name                      JobTitle AgencyID                        Agency               HireDate
1      Aaron,Patricia G Facilities/Office Services II   A03031      OED-Employment Dev (031) 10/24/1979 12:00:00 AM
2         Aaron,Petra L    ASSISTANT STATE'S ATTORNEY   A29045 States Attorneys Office (045) 09/25/2006 12:00:00 AM
3        Abbey,Emmanuel         CONTRACT SERV SPEC II   A40001     M-R Info Technology (001) 05/01/2013 12:00:00 AM
4  Abbott-Cole,Michelle        Operations Officer III   A90005           TRANS-Traffic (005) 11/28/2014 12:00:00 AM
5    Abdal-Rahim,Naim A   EMT Firefighter Suppression   A64120         Fire Department (120) 03/30/2011 12:00:00 AM
6 Abdelmeguid,Shahrazad         CONTRACT SERV SPEC II   A29010 States Attorneys Office (010) 11/30/2015 12:00:00 AM
  AnnualSalary  GrossPay
1    $56705.00 $54135.44
2    $75500.00 $72445.87
3    $60060.00 $59602.58
4    $70000.00 $59517.21
5    $64365.00 $74770.82
6    $40019.00 $16283.26
> 

此时,您可以使用write.xport(salaries,...)生成SAS导出文件。