-
Notifications
You must be signed in to change notification settings - Fork 2
/
textfileParse.Rmd
40 lines (29 loc) · 1.13 KB
/
textfileParse.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
---
title: "R Notebook"
output: html_notebook
---
```{r}
## Read log file
mylog <- readLines("./reputation")
mylog <- mylog[3:length(mylog)] # Remove header line and first line which is not date, start from date.
dateLines <- grep(x = mylog, pattern = "^--", value = TRUE)
dateLinesIdx <- grep(x = mylog, pattern = "^--")
repLines <- grep(x = mylog, pattern = "^--", value = TRUE, invert = TRUE)
repLinesIdx <- grep(x = mylog, pattern = "^--", value = FALSE, invert = TRUE)
## Convert to table
repTable <- read.table(text = repLines)
## Add column names
names(repTable) <- c("id", "otherid", "points")
## Extract points
require(stringr)
repTable$points <- with(repTable, as.numeric(str_extract(repTable$points, "[0-9]+"))) # (42) -> 42
## How many last actions no final date boundary.
lastActionsCount <- repLinesIdx[length(repLinesIdx)] - dateLinesIdx[length(dateLinesIdx)]
## Very cool diff per date.
actionsPerDay <- c(diff(dateLinesIdx) - 1, lastActionsCount)
## Convert to date.
dates = as.Date(str_extract(dateLines, "\\d{4}-\\d{2}-\\d{2}"))
## Add new column named date.
repTable$date <- rep(dates, times = actionsPerDay)
repTable
```