京公网安备 11010802034615号
经营许可证编号:京B2-20210330
用R语言统计用户登录信息
#设置统计时间段 格式: yyyy-mm-dd-hh-mm-ss
#如: 2013-09-10-00-00-00
start.time = "2013-09-01-00-00-00"
stop.time = "2013-10-13-00-00-00"
#伪装用户名
fake = T
library(lattice)
file = "login.log"
lines = readLines(con=file)
data = NULL
in.time = NULL
ch2d = function(x=NULL)
{
if(x=="Jan") return(1)
if(x=="Feb") return(2)
if(x=="Mar") return(3)
if(x=="Apr") return(4)
if(x=="May") return(5)
if(x=="Jun") return(6)
if(x=="Jul") return(7)
if(x=="Aug") return(8)
if(x=="Sep") return(9)
if(x=="Oct") return(10)
if(x=="Nov") return(11)
if(x=="Dec") return(12)
}
for( i in lines)
{
if( grepl(pattern="still",x=i)
| grepl(pattern="reboot",x=i)
| grepl(pattern="tty",x=i)
| grepl(pattern="wtmp",x=i)
| grepl(pattern="crash",x=i)
| grepl(pattern="down",x=i)
| i=="")
next
tmp = unlist(strsplit(x=i,split=" "))
tmp = tmp[tmp!=""]
tmp[10] = gsub(pattern="\\(",replacement="",x=tmp[10])
tmp[10] = gsub(pattern="\\)",replacement="",x=tmp[10])
tmp = c(tmp,unlist(strsplit(x=tmp[7],split=":")),unlist(strsplit(x=tmp[9],split=":")))
if(length(unlist(strsplit(x=tmp[10],split="\\+")))==2)
{ http://cda.pinggu.org/view/4496.html
day = unlist(strsplit(x=tmp[10],split="\\+"))[1]
tmp[10] = unlist(strsplit(x=tmp[10],split="\\+"))[2]
} else day = 0
hour = unlist(strsplit(x=tmp[10],split=":"))[1]
min = unlist(strsplit(x=tmp[10],split=":"))[2]
time = as.numeric(day) * 24 * 60 + as.numeric(hour) * 60 + as.numeric(min)
in.time = c(in.time,time)
rm(time)
data = rbind(data,tmp)
}
login.time = ISOdatetime(year=2013,month=lapply(X=data[,5],FUN=ch2d),day=data[,6],hour=data[,11],min=data[,12],sec=0)
rownames(data) = 1:nrow(data)
data = data.frame(data[,c(1,3:6,11,12)],in.time)
colnames(data) = c("user","IP","week","month","day","hour","min","time")
# 筛选统计时间段
start.time = as.numeric(unlist(strsplit(x=start.time,split="-")))
stop.time = as.numeric(unlist(strsplit(x=stop.time,split="-")))
start.time = ISOdatetime(year=start.time[1],month=start.time[2],
day=start.time[3],hour=start.time[4],
min=start.time[5],sec=start.time[6])
stop.time = ISOdatetime(year=stop.time[1],month=stop.time[2],
day=stop.time[3],hour=stop.time[4],
min=stop.time[5],sec=stop.time[6])
data = data[login.time>=start.time&login.time<=stop.time,]
print(paste(nrow(data),"records after filter."),quote=F)
#伪装用户名
if( fake == T )
{
# fake.name = matrix(sample(100:120,length(levels(data$user))*9,replace=T),ncol=9)
# fake.name = apply(fake.name,1,function(x)paste(intToChar(x),collapse=""))
fake.name = rep("",length=nrow(data))
for( i in unique(data$user))
{
fake.name[data$user==i] = paste(intToChar(sample(100:120,9)),collapse="")
}
data = cbind(data,fake.name)
data = data[,c(9,2:8)]
}
colnames(data) = c("user","IP","week","month","day","hour","min","time")
#统计每个用户登录时间数
time.per.user = data.frame(user=character(),time=numeric())
for( i in unique(data$user))
{
time.per.user = rbind(time.per.user,data.frame(user=i,time=sum(in.time[data$user==i])))
}
time.per.user = time.per.user[order(time.per.user$time),]
tp1 = barchart(time~user,time.per.user,scale=list(x=list(rot=90)),ylab="Time(minutes)")
#统计局域网外的ip登录数
IP.info = data$IP
IP.info = IP.info[!grepl(pattern="^10",x=IP.info)]
IP.info = IP.info[!grepl(pattern="^192",x=IP.info)]
IP.info = IP.info[!grepl(pattern="cu",x=IP.info)]
IP.info = IP.info[!grepl(pattern="io",x=IP.info)]
IP.info = IP.info[!grepl(pattern=":",x=IP.info)]
print(paste(" IP counts :",length(unique(IP.info))),quote=F)
#统计不同用户使用不同ip登录次数
tp2 = histogram(IP~user,data,scales=list(x=list(rot=90)))
plot(tp1,split=c(1,1,1,2))
plot(tp2,split=c(1,2,1,2),new=F)
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
在数字化时代,数据已成为企业决策的核心驱动力,数据分析与数据挖掘作为解锁数据价值的关键手段,广泛应用于互联网、金融、医疗 ...
2026-04-17在数据处理、后端开发、报表生成与自动化脚本中,将 SQL 查询结果转换为字符串是一项高频且实用的操作。无论是拼接多行数据为逗 ...
2026-04-17面对一份上万行的销售明细表,要快速回答“哪个地区卖得最好”“哪款产品增长最快”“不同客户类型的购买力如何”——这些看似复 ...
2026-04-17数据分析师一天的工作,80% 的时间围绕表格结构数据展开。从一张销售明细表到一份完整的分析报告,表格结构数据贯穿始终。但你真 ...
2026-04-16在机器学习无监督学习领域,Kmeans聚类因其原理简洁、计算高效、可扩展性强的优势,成为数据聚类任务中的主流算法,广泛应用于用 ...
2026-04-16在机器学习建模实践中,特征工程是决定模型性能的核心环节之一。面对高维数据集,冗余特征、无关特征不仅会增加模型训练成本、延 ...
2026-04-16在数字化时代,用户是产品的核心资产,用户运营的本质的是通过科学的指标监测、分析与优化,实现“拉新、促活、留存、转化、复购 ...
2026-04-15在企业数字化转型、系统架构设计、数据治理与AI落地过程中,数据模型、本体模型、业务模型是三大核心基础模型,三者相互支撑、各 ...
2026-04-15数据分析师的一天,80%的时间花在表格数据上,但80%的坑也踩在表格数据上。 如果你分不清数值型和文本型的区别,不知道数据从哪 ...
2026-04-15在人工智能与机器学习落地过程中,模型质量直接决定了应用效果的优劣——无论是分类、回归、生成式模型,还是推荐、预测类模型, ...
2026-04-14在Python网络编程、接口测试、爬虫开发等场景中,HTTP请求的发送与响应处理是核心需求。Requests库作为Python生态中最流行的HTTP ...
2026-04-14 很多新人学完Python、SQL,拿到一张Excel表还是不知从何下手。 其实,90%的商业分析问题,都藏在表格的结构里。 ” 引言:为 ...
2026-04-14在回归分析中,因子(即自变量)的筛选是构建高效、可靠回归模型的核心步骤——实际分析场景中,往往存在多个候选因子,其中部分 ...
2026-04-13在机器学习模型开发过程中,过拟合是制约模型泛化能力的核心痛点——模型过度学习训练数据中的噪声与偶然细节,导致在训练集上表 ...
2026-04-13在数据驱动商业升级的今天,商业数据分析已成为企业精细化运营、科学决策的核心手段,而一套规范、高效的商业数据分析总体流程, ...
2026-04-13主讲人简介 张冲,海归统计学硕士,CDA 认证数据分析师,前云南白药集团资深数据分析师,自媒体 Python 讲师,全网课程播放量破 ...
2026-04-13在数据可视化与业务分析中,同比分析是衡量业务发展趋势、识别周期波动的核心手段,其核心逻辑是将当前周期数据与上年同期数据进 ...
2026-04-13在机器学习模型的落地应用中,预测精度并非衡量模型可靠性的唯一标准,不确定性分析同样不可或缺。尤其是在医疗诊断、自动驾驶、 ...
2026-04-10数据本身是沉默的,唯有通过有效的呈现方式,才能让其背后的规律、趋势与价值被看见、被理解、被运用。统计制图(数据可视化)作 ...
2026-04-10在全球化深度发展的今天,跨文化传播已成为连接不同文明、促进多元共生的核心纽带,其研究核心围绕“信息传递、文化解读、意义建 ...
2026-04-09