Skip to content

Instantly share code, notes, and snippets.

@CaptainJH
Created March 24, 2014 06:31
Show Gist options
  • Save CaptainJH/9735184 to your computer and use it in GitHub Desktop.
Save CaptainJH/9735184 to your computer and use it in GitHub Desktop.
主要是统计用户上传信息的 r 代码
ReadUserInfoFunc <- function(file)
{
#print(file)
ret <- read.table(file, header=TRUE, sep=",")
return (ret)
}
SplitIntoBox <- function(num, split)
{
for(j in 1:(length(split) + 1))
{
if(j == 1)
{
if(num < split[j])
{
return (j)
}
}
else if(j == length(split) + 1)
{
if(num >= split[j - 1])
{
return (j)
}
}
else
{
if(num >= split[j - 1] && num < split[j])
{
return (j)
}
}
}
}
SplitIntoBoxExact <- function(num, split)
{
for(j in 1:length(split))
{
if(num == split[j])
{
return (j)
}
}
}
SplitIntoBoxExactGrep <- function(text, split)
{
for(j in 1:length(split))
{
if(grepl(split[j], text, ignore.case = TRUE))
{
return (j)
}
}
}
AnalysisVCardVenderFunc <- function(frame)
{
split <- c("amd", "nvidia", "intel")
count <- c()
labs <- c()
for(i in 1:length(split))
{
count[i] <- 0
labs[i] <- 0
}
for(l in 1:nrow(frame))
{
os = frame[l, 11]
index <- SplitIntoBoxExactGrep(os, split)
count[index] <- count[index] + 1
}
labs[1] <- sprintf("AMD, %4.1f%%", count[1] / sum(count) * 100)
labs[2] <- sprintf("nVidia, %4.1f%%", count[2] / sum(count) * 100)
if(count[3] > 0)
{
labs[3] <- sprintf("Intel, %4.1f%%", count[3] / sum(count) * 100)
}
print(count)
pie(count, labels=labs, main="user VCard Vender statistic")
}
AnalysisOSFunc <- function(frame)
{
split <- c("xp.+\\[", "7.+\\[")
count <- c()
labs <- c()
for(i in 1:length(split))
{
count[i] <- 0
labs[i] <- 0
}
for(l in 1:nrow(frame))
{
os = frame[l, 3]
index <- SplitIntoBoxExactGrep(os, split)
count[index] <- count[index] + 1
}
labs[1] <- sprintf("Win XP, %4.1f%%", count[1] / sum(count) * 100)
labs[2] <- sprintf("Win 7, %4.1f%%", count[2] / sum(count) * 100)
print(count)
pie(count, labels=labs, main="user OS statistic")
}
AnalysisOSBitFunc <- function(frame, split)
{
count <- c()
labs <- c()
for(i in 1:length(split))
{
count[i] <- 0
labs[i] <- 0
}
for(l in 1:nrow(frame))
{
temp = frame[l, 4]
ch = as.character(temp)
c <- strsplit(ch, " ")
ch1 = c[[1]]
num = as.numeric(ch1[1])
index <- SplitIntoBoxExact(num, split)
count[index] <- count[index] + 1
}
for(j in 1:length(count))
{
if(j == 1)
{
labs[j] <- sprintf("32 Bit, %4.1f%%", count[j] / sum(count) * 100)
}
else if(j == length(count))
{
labs[j] <- sprintf("64 Bit, %4.1f%%", count[j] / sum(count) * 100)
}
}
print(count)
pie(count, labels=labs, main="user OS Bits statistic")
}
AnalysisCPUFunc <- function(frame, split)
{
count <- c()
labs <- c()
for(i in 1:(length(split) + 1))
{
count[i] <- 0
labs[i] <- 0
}
for(l in 1:nrow(frame))
{
temp = frame[l, 8]
ch = as.character(temp)
num = as.numeric(ch)
index <- SplitIntoBox(num, split)
#print(index)
count[index] <- count[index] + 1
}
for(j in 1:length(count))
{
if(j == 1)
{
labs[j] <- sprintf("< %4.0f Core, %4.1f%%", split[j], count[j] / sum(count) * 100)
}
else if(j == length(count))
{
labs[j] <- sprintf(">= %4.0f Core, %4.1f%%", split[j - 1], count[j] / sum(count) * 100)
}
else
{
labs[j] <- sprintf("%4.0f Core <= <%4.0f Core, %4.1f%%", split[j - 1], split[j], count[j] / sum(count) * 100)
}
}
print(count)
pie(count, labels=labs, main="user CPU statistic")
}
AnalysisMemoryFunc <- function(frame, split)
{
count <- c()
labs <- c()
for(i in 1:(length(split) + 1))
{
count[i] <- 0
labs[i] <- 0
}
for(l in 1:nrow(frame))
{
temp = frame[l, 5]
ch = as.character(temp)
c <- strsplit(ch, " ")
ch1 = c[[1]]
num = as.numeric(ch1[1])
index <- SplitIntoBox(num, split)
count[index] <- count[index] + 1
}
for(j in 1:length(count))
{
if(j == 1)
{
labs[j] <- sprintf("< %4.0f MB, %4.1f%%", split[j], count[j] / sum(count) * 100)
}
else if(j == length(count))
{
labs[j] <- sprintf(">= %4.0f MB, %4.1f%%", split[j - 1], count[j] / sum(count) * 100)
}
else
{
labs[j] <- sprintf("%4.0f MB <= <%4.0f MB, %4.1f%%", split[j - 1], split[j], count[j] / sum(count) * 100)
}
}
print(count)
pie(count, labels=labs, main="user memory statistic")
}
userInfo <- ReadUserInfoFunc("C:/Users/heqiju/Desktop/硬件信息dat/userdata.csv")
#AnalysisCPUFunc(userInfo, c(8))
#AnalysisMemoryFunc(userInfo, c(4000, 4010))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment