1.用键盘输入数据;
2.通过读取存储在外部文件上的数据;
3.通过访问数据库系统来获取数据。
一、用键盘输入数据
当遇到纸质版表格,需将纸质版的数据输入到系统中。
patientID <- c(1,2,3,4)
admdate <- c("10/15/2009","11/01/2009","10/21/2009","10/28/2009")
age <- c(25,34,28,52)
diabetes <- c("Type1","Type2","Type1","Type1")
status <- c("Poor","Improved","Excellent","Poor")
data <- data.frame(patientID,admdate,age,diabetes,status)
data
调用编辑器输入数据,这里一定要将数据赋值回原数据
二、通过读取存储在外部文件上的数据
# 设置工作目录,可以直接通过文件名访问文件
setwd()
read.table("input.txt")
#方法二,通过文件的绝对路径访问文件
read.table("C:/Users/乐享/Documents/Rdata/input.txt")
#访问前6行
head()
#访问后6行
tail()
- read.table()访问CSV格式的文件时,要加上 sep=","
- header="TRUE",第一行是表头
- 参数skip,读取文件时,跳过部分内容
- read.table()
读取非文本文件
XML包
三、写入文件
write(x,file = "x.txt")
四、通过访问数据库系统来获取数据
通过RODBC