問題
你想從文件中載入數(shù)據(jù)。
方案
帶分隔符的文本文件
最簡單的輸入數(shù)據(jù)的方式就是將其保存為帶分隔符(如:制表位或逗號)的文本文件。
data <- read.csv("datafile.csv")
# 導入一個沒有表頭的CSV文件
data <- read.csv("datafile-noheader.csv", header=FALSE)
函數(shù)read.table()是一個更為普遍的函數(shù),允許你設置分隔符,不管是否有表頭,不管字符串是否有引號,等等。使用?read.table查看更多詳細信息。
data <- read.table("datafile-noheader.csv",
header=FALSE,
sep="," # 制表位分隔的文件用"\t"
)
打開文件的文件選擇器
某些平臺,使用file.choose()可以打開文件選擇的對話窗口;其他平臺則會提示用戶輸入一個文件名。
data <- read.csv(file.choose())
把字符串看作因子(factor)或字符(character)
默認條件下,數(shù)據(jù)中的字符串都被轉換為因子。如果你用read.csv()載入數(shù)據(jù),所有的文本列都會被視為因子,即便它被處理為字符串更有意義。要這么做,使用 stringsAsFactors=FALSE:
data <- read.csv("datafile.csv", stringsAsFactors=FALSE)
# 將某一列轉化為因子
data$Sex <- factor(data$Sex)
另一種將他們加載為因子,把某一列轉換為字符的方法:
data <- read.csv("datafile.csv")
data$First <- as.character(data$First)
data$Last <- as.character(data$Last)
# 另一種方法:轉化名為“First”和“Last”的兩列
stringcols <- c("First","Last")
data[stringcols] <- lapply(data[stringcols], as.character)
從網(wǎng)上導入文件
也可以從URL加載數(shù)據(jù)。這些(很長的)URL可以加載相關文件。
data <- read.csv("http://www.cookbook-r.com/Data_input_and_output/Loading_data_from_a_file/datafile.csv")
# 讀取沒有表頭的CSV文件
data <- read.csv("http://www.cookbook-r.com/Data_input_and_output/Loading_data_from_a_file/datafile-noheader.csv", header=FALSE)
# 手動添加表頭
names(data) <- c("First","Last","Sex","Number")
上述所使用的數(shù)據(jù)文件:
"First","Last","Sex","Number"
"Currer","Bell","F",2
"Dr.","Seuss","M",49
"","Student",NA,21
"Currer","Bell","F",2
"Dr.","Seuss","M",49
"","Student",NA,21
定寬文本文件
假如你的數(shù)據(jù)列寬固定,如下例:
First Last Sex Number
Currer Bell F 2
Dr. Seuss M 49
"" Student NA 21
讀取這種數(shù)據(jù)的一種方式是簡單地使用read.table()函數(shù)strip.white=TRUE,可以清除額外的空格。
read.table("clipboard", header=TRUE, strip.white=TRUE)
然而,你的數(shù)據(jù)文件可能包含空間列,或列沒有空格分開,這樣,scores列表示六個不同的測量值,每一個從0到3。
subject sex scores
N 1 M 113311
NE 2 F 112231
S 3 F 111221
W 4 M 011002
這種情況,你可能需要使用read.fwf()函數(shù)。如果你讀的列名來自于文件,它要求他們用分隔符(如:制表位,空格,逗號)分開。如果有多個空格分開將他們分開,如下例,你需要直接指定列的名稱。
# 指定列的名稱
read.fwf("myfile.txt",
c(7,5,-2,1,1,1,1,1,1), # 列的寬度,-2意味著放棄這些列
skip=1, # 跳過第一行(包括表頭)
col.names=c("subject","sex","s1","s2","s3","s4","s5","s6"),
strip.white=TRUE) # 跳過每個數(shù)據(jù)的前導和尾隨
#> subject sex s1 s2 s3 s4 s5 s6
#> 1 N 1 M 1 1 3 3 1 1
#> 2 NE 2 F 1 1 2 2 3 1
#> 3 S 3 F 1 1 1 2 2 1
#> 4 W 4 M 0 1 1 0 0 2
# subject sex s1 s2 s3 s4 s5 s6
# N 1 M 1 1 3 3 1 1
# NE 2 F 1 1 2 2 3 1
# S 3 F 1 1 1 2 2 1
# W 4 M 0 1 1 0 0 2
# 如果第一行如下:
# subject,sex,scores
# 我們可以使用header=TRUE
read.fwf("myfile.txt", c(7,5,-2,1,1,1,1,1,1), header=TRUE, strip.white=TRUE)
#> Error in read.table(file = FILE, header = header, sep = sep, row.names = row.names, : more columns than column names
# 錯誤:列比例名多
Excel文件
gdata包里的read.xls函數(shù)可以讀取Excel文件。
library(gdata)
data <- read.xls("data.xls")
gdata包,見http://cran.r-project.org/doc/manuals/R-data.html#Reading-Excel-spreadsheets.
包的安裝,見Basics-安裝和使用R包
SPSS數(shù)據(jù)
foreign包里的read.spss函數(shù)可以讀取SPSS文件。
library(foreign)
data <- read.spss("data.sav", to.data.frame=TRUE)
原文鏈接:http://www.cookbook-r.com/Data_input_and_output/Loading_data_from_a_file/