首页 > 解决方案 > R用read.csv读取csv文件

问题描述

使用此脚本:

# I) Go to the working directory
setwd("/home/***/Desktop/***")

# II) Verify the current working directory
print(getwd())

# III) Load te nedded package
require("csv")

# IV) Read the desired file
read.csv(file="serious-injury-outcome-indicators-2000-18-csv.csv", header=TRUE, sep=",")

我试图阅读此文件: 严重伤害结果指标:2000–18 – CSV

但是控制台中的输出并不好,有人可以尝试一下并告诉我他是否遇到了同样的问题。谢谢

标签: rcsv

解决方案


您可以直接从网站读取文件:

dta <- url("https://www.stats.govt.nz/assets/Uploads/Serious-injury-outcome-indicators/Serious-injury-outcome-indicators-200018/Download-data/serious-injury-outcome-indicators-2000-18-csv.csv")
injury <- read.csv(dta, header=TRUE)
str(injury)
# 'data.frame': 2460 obs. of  13 variables:
#  $ Series_reference: chr  "W_A11" "W_A11" "W_A11" "W_A11" ...
#  $ Period          : chr  "2000-02" "2001-03" "2002-04" "2003-05" ...
#  $ Type            : chr  "Moving average" "Moving average" "Moving average" "Moving average" ...
#  $ Data_value      : num  59.7 60 59 59 61.3 ...
#  $ Lower_CI        : num  50.9 51.2 50.3 50.3 52.5 ...
#  $ Upper_CI        : num  68.4 68.8 67.7 67.7 70.2 ...
#  $ Units           : chr  "Injuries" "Injuries" "Injuries" "Injuries" ...
#  $ Indicator       : chr  "Number" "Number" "Number" "Number" ...
#  $ Cause           : chr  "Assault" "Assault" "Assault" "Assault" ...
#  $ Validation      : chr  "Validated" "Validated" "Validated" "Validated" ...
#  $ Population      : chr  "Whole pop" "Whole pop" "Whole pop" "Whole pop" ...
#  $ Age             : chr  "All ages" "All ages" "All ages" "All ages" ...
#  $ Severity        : chr  "Fatal" "Fatal" "Fatal" "Fatal" ...

推荐阅读