R初学,边学边写博客。日常小应用。R处理exel。

来源:互联网 时间:2015-12-05

有几个excel表,但是每个表中的数据都不一样,所以读进R里面进行简单的处理。过程很简单,基本是数据筛选,重命名,添加列还有合并。还包含了读取表和存储表的注意事项。

#百度setwd("e:/baidu")#设置工作目录a<-read.table("20151125.csv",sep=",",fill=T,skip=7,header = T)#读取文件,跳过空白a[,3]<-"baidu"#修改第三列的内容b<-read.table("20151125(1).csv",sep=",",fill=T,skip=7,header = T)b[,3]<-"baixingwang"c<-rbind(a,b)#合并两表baidu<-data.frame(c$日期,c$账户,c$推广计划,c$展现,c$点击,c$消费)#挑选有用变量

colnames(baidu)=c("日期","账户","推广计划","展现","点击","消费")#重新命名#搜狗setwd("e:/sogou")d<-read.table("[搜狗推广服务]定制报告-搜狗推广报告-2015-11-25_全设备.csv",sep=",",fill=T,header = T)d<-d[-1,]sogou<-data.frame(d$日期,d$账户,d$推广计划,d$展示数,d$点击数,d$消耗)sogou[,2]<-"sogou"colnames(sogou)=c("日期","账户","推广计划","展现","点击","消费")#360点睛setwd("e:/360")e<-read.table("2015-11-24推广计划数据报告.csv",sep=",",fill=T,header = T)日期<-c(1:length(e$推广计划))账户<-c(1:length(e$推广计划))e<-data.frame(e,日期,账户)e$日期<-"2015/11/24"e$账户<-"360点睛"dianjing<-data.frame(e$日期,e$账户,e$推广计划,e$展示次数,e$点击次数,e$总费用)dianjing[,2]<-"360"colnames(dianjing)=c("日期","账户","推广计划","展现","点击","消费")#神马=============setwd("e:/shenma")library(xlsx)f<-read.xlsx("计划推广报告-2015-11-25~2015-11-25#2015-11-26+15-46-38.xlsx",1,encoding='UTF-8')f<-f[,1:6]shenma<-data.frame(f)shenma[,2]<-"shenma"colnames(shenma)=c("日期","账户","推广计划","展现","点击","消费")#合并============all<-rbind(baidu,sogou,dianjing,shenma)#纵向合并setwd("e:/output")write.csv(all,"四渠道",row.names = FALSE)#与四渠道合并setwd("e:/ga")#quote="/""认为只有双引号才分隔,这样Xi’an可以在一起g<-read.table("Analytics 全站数据 二手房整站页 (计划) 20151030-20151129.csv",sep=",",fill=T,header = T,skip=6,quote = "\"")m<-merge(g,all,by="推广计划",all=T)setwd("e:/output")write.csv(m,"四渠道匹配.csv")#读取GA数据=============================================#全站pv导航============================setwd("e:/ga")b<-read.table("Analytics 全站数据 PV 导航 20151116-20151122.csv",fill=T,sep=",",header = F,skip=7)b<-b[1:(which(b$V1=="")[1]-1),]#根据第一列的长度来截取数据。因为这个表笔记特殊,上面是四列,下面两列,两列的数据用不到。colnames(b)=c("来源","会话","新会话百分比","新用户","跳出率","每次会话浏览页数","平均会话时长","用户数","浏览量")

相关阅读:
Top