資料讀取
單個檔案資料讀取
讀取txt格式資料
df <- read.table("xu.txt",header= TRUE)
讀取xlsx的檔案
library(xlsx)
df <- read.xlsx("xu.xlsx",1)
讀取網頁表格
補充
windows和Linux的檔案路徑
(1)windows的檔案路徑格式“E:Pythonworkplacecodes”單反斜杠的方式,但是在很多程式設計語言中會不認識“”字元,可能會把它識别成轉譯字元,通常我們在windows引用路徑的時候需要用“\”來表示“”,windows的檔案路徑的正确寫法在程式中最好是“E:\Python\workplace\codes”。
(2)linux的檔案路徑格式為“python/workplace/codes”這種單斜杠的方式,這種路徑很多時候是可以适用在windows下的,但是會有一定的弊端,當路徑中有空格的時候這種路徑方式就不适用與windows
如果是想抓去網頁上的某個表格,那麼可以使用XML包中的readHTMLTable()函數。例如我們想獲得google統計的通路最多的1000名網站資料,則可以象下面這樣做。關于這個函數可以參考這篇博文。
url <- '
http://www.google.com/adplanner/static/top1000/'
data <- readHTMLTable(url)
names(data)
head(data[[2]])
批量讀取本地檔案
在批量讀取文檔時一般先将其存放在某一個目錄下。先用dir()函數擷取目錄中的檔案名,然後用paste()将路徑合成,最後用循環或向量化方法處理文檔。
doc.names <- dir("path")
doc.path <- sapply(doc.names,function(names) paste(path,names,sep='/'))
doc <- sapply(doc.path, function(doc) readLines(doc))
将一個檔案夾下的多個相同的檔案合并起來
library(dplyr)
nameList <- list.files("GSE48213_RAW/") # list.files用于列出檔案夾下所包含的檔案名
matrix <- read.table(paste0("GSE48213_RAW/",nameList[1]),header = T)
for (i in 2:length(nameList)){
matrix <- inner_join(matrix,
read.table(paste0("GSE48213_RAW/",nameList[i]),header = T),
by="EnsEMBL_Gene_ID")
}
save(matrix,file = "56_cell_expression.Rda")
資料庫連接配接取數
library(RMySQL)
connectMySQL<-function(mysql,dbname,user,password,host){
drv<-dbDriver(mysql)
return(dbConnect(drv,dbname,user,password,host))
}
connect <- function()
{
con <- connectMySQL(mysql = "MySQL", dbname = "test", user = "***", password = "A", host = "10.***")
return(con)
}
con=connect()
news=dbGetQuery(con,"select news_content from news_cms where substr(news_time,1,10)='2016-05-09';")
輸出資料
write.table(AAPL, file = "E:/R腳本/data.csv", append =FALSE)
與資料庫相連輸出資料
con<-connect()
dbWriteTable(con, "cookie_adlog_2", ad_msg,row.names =FALSE, overwrite = TRUE)
overwrite: 一個邏輯值,是否覆寫表中的資料,預設為FALSE
append: 一個邏輯值,是否是追加資料到已存在的表,預設為FALSE
儲存為逗号分割文本
write.csv(d, file = "c:/data/foo.csv", row.names = F, quote = F)
儲存為R格式檔案
save(d, file = "c:/data/foo.Rdata")
存到本地
save(I,file="/opt/mllib_data/futureKPI/futureUserIndex.dat")
通過R在mysql建表
data_delete<-dbGetQuery(conn,str_c("CREATE TABLE ss_usr_trd_anlytcs
(
dy varchar(8) comment '時間',
eqt_id varchar(500) comment '使用者名',
rgstr_date varchar(50) comment '注冊時間',
fst_trd_date varchar(50) comment '首次成交時間',
lst_trd_date varchar(50) comment '最近1次成交時間',
lst_trd_amt double comment '最近1次成交金額',
trd_sccss_cnt_lst_mnth int comment '最近1個月累計成交次數',
trd_amt_lst_mnth double comment '最近1個月累計成交金額',
trd_sccss_cnt_thr_mnth int comment '最近3個月累計成交次數',
trd_amt_thr_mnth double comment '最近3個月累計成交金額',
accmltd_trd_sccss_cnt int comment '累計成交次數',
accmltd_trd_amt double comment '累計成交金額',
trd_cnt_lst_mnth int comment '最近1個月累計交易次數',
ss_usr_type text comment '使用者類型',
ss_strtg_prdct text comment '購買的及購買次數和總金額數',
ss_strtg_trnd_wght text comment '按次數、金額、時間計算産品類别及其權重',
strtg_type text comment '類型及其次數',
ss_prdct_type text comment '産品類别及其購買次數 ',
ss_strtgs_lvl text comment '等級及次數'
)
ENGINE=MyISAM DEFAULT CHARSET=utf8", collapse = ""))