2016年6月24日金曜日

Use list as associative array part 2

This one is better than the previous one.

> test2_list <- list(list("Mon","Tue","Wed","Thu","Fri","Sat","Sun"),list(1,2,3,4,5,6,7))
> for(i in test2_list[[2]]){
+   # if(match(weekdays(as.Date(ISOdate(year(Sys.Date()),1,1)),abbreviate = TRUE),test_list[[1]][i],nomatch = FALSE))
+   print(paste(">>",test2_list[[1]][i]))
+ }
[1] ">> Mon"
[1] ">> Tue"
[1] ">> Wed"
[1] ">> Thu"
[1] ">> Fri"
[1] ">> Sat"
[1] ">> Sun"

Use list as associative array.


plase also see part2.

utilize a list as associative array.

1)create a list which each element contains a pair of information.

> test_list <- list(list("Mon",1),list("Tue",2),list("Wed",3),list("Thu",4),list("Fri",5),list("Sat",6),list("Sun",7))

2)when weekday returns day of the first day of the year, numeric seq number which is associated with weekday's return will be printed.

> for(i in 1:length(test_list)){
   if(match(weekdays(as.Date(ISOdate(year(Sys.Date()),1,1)),abbreviate = TRUE),test_list[[i]][1],nomatch = FALSE))
     {print(test_list[[i]][2])}
 }
[[1]]
[1] 5

2016年6月15日水曜日

Calculate day dependent data with less workload - 2 - test 2 strings are equal or not

Use match function to test 2 strings are same or not.

Below is a normal case and easy.

> weekdays(as.Date("2016-01-01"),abbreviate = TRUE)
[1] "Fri"
match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Fri")
[1] 1

In other cases, match return NA not 0 by default and this causes a problem in if function. I see this is very strange, however, it is just one more option to go.

> match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Mon")
[1] NA

The parameter nomatch will give an option to set a value for the case. with "nomatch=0" you can booleanize the return value

> match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Fri", nomatch=0)
[1] 1
> match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Thur", nomatch=0)
[1] 0

Then now it's possible to use if-then-else clause without problem.

> if(match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Mon",nomatch=0)){print("金曜日")}else{print("other days...")}
[1] "other days..."
> if(match(weekdays(as.Date("2016-01-01"),abbreviate = TRUE),"Fri",nomatch=0)){print("金曜日")}else{print("other days...")}
[1] "金曜日"

how to eliminate blank lines by atom regex.

​use "^\s" instead of traditonal "^$". seems that atom's regex works only ​​when at least one character is being consumed.

When given data are below,

[2016-06-01,167],[2016-06-02,155],[2016-06-03,140],[2016-06-04,138],[2016-06-05,144],[2016-06-06,176],[2016-06-07,157],[2016-06-08,146],[2016-06-09,134],[2016-06-10,130],[2016-06-11,119],[2016-06-12,107],[2016-06-13,141]]

regex below will remove all brackets and leave data and just one comma before them. DO NOT FORGET BRACES!

\[+[0-9-]+,([0-9]+)\][,\]]  →  $1,

when original data is as below

423 258 314 175 255 211 134 162 205 168 156 273 258 288 291 243 128

(\b[0-9]*\b) → $1,

wil add comma at the end of each numeric.

423, 258, 314, 175, 255, 211, 134, 162, 205, 168, 156, 273, 258, 288, 291, 243, 128,

77.9 73.5 74.5 72.2 73.1 73.4 72.9 72.7 76.9 73.1 71.8 79 79.9 78.4 77.4 79.6 74.6

(\s) → ,   # actually ", + space"

77.9, 73.5, 74.5, 72.2, 73.1, 73.4, 72.9, 72.7, 76.9, 73.1, 71.8, 79, 79.9, 78.4, 77.4, 79.6, 74.6


2016年6月14日火曜日

Calculate # of months from the beginning of the year.

change language setting causes in other parts of system. the example is below.

> m <- as.numeric(months(Sys.Date(),abbreviate = T)) 

in JA environment, this operation returns 6 in June but ,in EN , months returns "Jun" and it is unable to process in as.numeirc. This breaks operations in the latter parts.

The fix is to use MonthsBetween and mondate in mondate library. They should be independent from language setting.

m <- ceiling(MonthsBetween(mondate("2016-01-01"),mondate(Sys.Date()))) ;
> m
[1] 6
attr(,"timeunits")
[1] "months"




2016年6月10日金曜日

Calculate day dependent data with less workload - 1 set languge locale


In order to calculate day dependent data with less workload, I like to automate processes. The first step is to judge the first day of each belongs to which day of the week. weekday function provides this function.

Currently R's language setting is set to JA. This may be

weekdays(as.Date("2016-01-01"),abbreviate = TRUE)
[1] "金"
> Sys.getlocale("LC_MESSAGES")
[1] "ja_JP.UTF-8"

Change LC_MESSAGES to US. But weekdays function still return the value in Japanese.

> Sys.setlocale("LC_MESSAGES",'en_US')
[1] "en_US"
> weekdays(as.Date("2016-01-01"),abbreviate = TRUE)
[1] "金"

Investigate Sys.setlocale and found that there are other classes than LC_MESSAGES. I have no problem with US English.

> Sys.setlocale
function (category = "LC_ALL", locale = "") 
{
    category <- match(category, c("LC_ALL", "LC_COLLATE", "LC_CTYPE", 
        "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LC_MESSAGES", 
        "LC_PAPER", "LC_MEASUREMENT"))
    if (is.na(category)) 
        stop("invalid 'category' argument")
    .Internal(Sys.setlocale(category, locale))
}
<bytecode: 0x102cfdee0>
<environment: namespace:base>

Set LC_ALL locale, instead of LC_MESSAGES to US English.

> Sys.setlocale("LC_ALL",'en_US')
[1] "en_US/en_US/en_US/C/en_US/en_US"

Then return is in US English.

> weekdays(as.Date("2016-01-01"),abbreviate = TRUE)
[1] "Fri"
>

2016年6月9日木曜日

S&P 500 according to GDP forecast by GDPnow 2016-05-28


Now the target value at the end of 2016 is 2339.

> 0.603*(predict(VAR(v_XCPG_1992_2016,lag=6))$fcst$GDP[,1])-7791
 [1] 2249.308 2266.049 2280.233 2294.785 2306.989 2318.216 2328.660 2339.162 2349.197 2358.947

2016年6月8日水曜日

Set Operator - count working days in 2016

Combined previous two samples will bring the list which are weekday bu not holiday.

> seq(1,365,1)[is.element(seq(1,365,1)%%7,c(6,4,5,0,1))][!is.element(seq(1,365,1)[is.element(seq(1,365,1)%%7,c(6,4,5,0,1))],holiday2016)]


Then you now know how many days you have to go to the office.

> length(seq(1,365,1)[is.element(seq(1,365,1)%%7,c(6,4,5,0,1))][!is.element(seq(1,365,1)[is.element(seq(1,365,1)%%7,c(6,4,5,0,1))],holiday2016)])
[1] 245

2016年6月7日火曜日

Set operator - how to create the list of Monday to Friday seq number for 2016.


Create the list of seq number of weekday for 2016. Here 4 represents Monday. 5 Tuesday, 6 Thursday and 0 for Friday.

> seq(1,365,1)[is.element(seq(1,365,1)%%7,c(6,4,5,0,1))]
  [1]   1   4   5   6   7   8  11  12  13  14  15  18  19  20  21  22  25  26  27  28  29  32  33  34  35  36  39  40  41
 [30]  42  43  46  47  48  49  50  53  54  55  56  57  60  61  62  63  64  67  68  69  70  71  74  75  76  77  78  81  82
 [59]  83  84  85  88  89  90  91  92  95  96  97  98  99 102 103 104 105 106 109 110 111 112 113 116 117 118 119 120 123
 [88] 124 125 126 127 130 131 132 133 134 137 138 139 140 141 144 145 146 147 148 151 152 153 154 155 158 159 160 161 162
[117] 165 166 167 168 169 172 173 174 175 176 179 180 181 182 183 186 187 188 189 190 193 194 195 196 197 200 201 202 203
[146] 204 207 208 209 210 211 214 215 216 217 218 221 222 223 224 225 228 229 230 231 232 235 236 237 238 239 242 243 244
[175] 245 246 249 250 251 252 253 256 257 258 259 260 263 264 265 266 267 270 271 272 273 274 277 278 279 280 281 284 285
[204] 286 287 288 291 292 293 294 295 298 299 300 301 302 305 306 307 308 309 312 313 314 315 316 319 320 321 322 323 326
[233] 327 328 329 330 333 334 335 336 337 340 341 342 343 344 347 348 349 350 351 354 355 356 357 358 361 362 363 364 365


2016年6月2日木曜日

Set Operator - how to extract only working day from all Friday of 2016.


When holiday2016 is the list of seq. number to represent national holidays and seq(1,365,7) is for all Friday in Friday
seq(1,365,7)[!is.element(seq(1,365,7),holiday2016)] will generate the list of sequence numbers for every working Friday in the year.

> holiday2016
 [1]   1   2  11  42  81 120 124 125 126 200 224 263 267 284 308 328 358
> seq(1,365,7)
 [1]   1   8  15  22  29  36  43  50  57  64  71  78  85  92  99 106 113 120 127 134 141 148 155 162 169 176 183 190 197
[30] 204 211 218 225 232 239 246 253 260 267 274 281 288 295 302 309 316 323 330 337 344 351 358 365
> seq(1,365,7)[!is.element(seq(1,365,7),holiday2016)]
 [1]   8  15  22  29  36  43  50  57  64  71  78  85  92  99 106 113 127 134 141 148 155 162 169 176 183 190 197 204 211
[30] 218 225 232 239 246 253 260 274 281 288 295 302 309 316 323 330 337 344 351 365