处理大批量文本数据详解.docxVIP

  • 11
  • 0
  • 约2.77千字
  • 约 7页
  • 2018-08-01 发布于湖北
  • 举报
如何批量处理文本文件最近数据堂为了弄数据挖掘比赛提供了一批用户行为日志数据。对于以前没玩过的数据,我是特别的好奇。处理这批文本文件确实花了不少时间。数据以不同的日期作文件夹分别存放,每个文件夹中又有近一千个文本文件,每个文件都是一个用户的行为日志。为了分析这些数据,首先需要将这两万个文本文件读入R中,再用字符串函数进行处理成结构化的数据。处理方法如下:library(stringr)setwd(D:\\kaggle\\tang\\data)data - read.table(data.csv,T,sep=,)?# 读入文档地址path - D:\\kaggle\\tang\\data\\behaviordir.name - dir(D:\\kaggle\\tang\\data\\behavior)doc.name - list()fre - numeric()for(i in1:length(dir.name)){ doc.name[[i]]- dir(paste(path,dir.name[i],sep=\\)) fre[i] - length(doc.name[[i]])}dir - rep(dir.name,fre)doc.name -unlist(doc.name)file.name - character()for( i in1:length(dir)){ file.name[i] - paste(path,dir[i],doc.name[i],sep=\\)}?# 建立抽取主函数,返回列表data.get - function(file.name){#获取文本 temp - readLines(file.name,encoding=UTF-8)if(length(temp)=2)return(NA)# 用户编号 id - str_match(file.name,[A-Z0-9]{32})# 调查日期 day - str_match(file.name,\\d+-\\d+-\\d+)# 开机时点 clock -str_sub(str_match(file.name,_\\d{2}-),2,3)# 切分转换文本函数 trans - function(x){ res - unlist(str_split(x,\\[=\\]|=))# res - str_extract(x, [^=]+\\.[a-z]+)return(res)}# 将文本切开转为列表 result - lapply(temp,trans)?# 开机时长 opentime - as.numeric(result[[1]][2])# 先进行时间分析,由于有的信息不是按顺序排列,所以要按时点重排# 时点抽取time - unlist(lapply(result[-c(1:2)],function(x) x[2]))time - as.numeric(time)# 时点排序,然后重排result列表 new.res - result[-c(1:2)][order(time)]?# 返回用户使用的程序向量 prog - unlist(lapply(new.res,function(x) x[4]))# 各程序使用时间time - unlist(lapply(new.res,function(x) x[2]))time - as.numeric(time)time[length(time)+1] - opentimetime - diff(time)? prog.time - data.frame(prog=prog,time=time) progtime - dcast(prog.time,prog~.,sum,value.var=time)names(progtime) - c(program,time)?# 使用的软件个数 numofsoft - nrow(progtime)# 真实使用时长 realtime - sum(progtime$time,na.rm=T)return(list(id=as.character(id),day=as.character(day), clock = as.numeric(clock),opentime=opentime, numofsoft = numofsoft, realtime= realtime,progtime=progtime))}所有的文件名都已经存在file.name变量中,用上面建

文档评论(0)

1亿VIP精品文档

相关文档