如何用R来算命



  • 只听得玉缘道人侃侃而谈:“文王拘而演《周易》,所假之物不过蓍草,大衍之数区区五十,流传至今竟然开枝散叶、门派林立,其中真真假假良莠不齐,贫道宁愿删繁就简追本溯源,我只与你用蓍草开出两个卦象,一为本卦,一为变卦,其余艰深晦涩的互卦、错卦、综卦一概不究,大人意下如何?”

    如下 R code 可以完美复制周易的蓍草算命法则,如需算命可以全选如下 code 运行,解释卦象可以自行百度 ^^
    注:本程序不作为投资指南,金融市场潮起潮落,买张买跌,盈亏自负,但求心安。

    require(data.table)
    require(stringr)
    
    zhanbu = function() {
      
      # round 1
      
      N = 50 - 1
      
      shicao = data.table(
        LHS = 12:(N-12),
        RHS = (N-12):12
      )
      
      sel = sample(1:shicao[, .N], 1)
      shicao = shicao[sel, ]
      
      shicao[, LHS := LHS - 1]
      shicao[, LHS_remainder := LHS %% 4]
      shicao[LHS_remainder == 0, LHS_remainder := 4]
      shicao[, LHS_remainder := LHS_remainder + 1]
      
      shicao[, RHS_remainder := RHS %% 4]
      shicao[RHS_remainder == 0, RHS_remainder := 4]
      
      shicao[, LHS_remainder + RHS_remainder] %in% c(5, 9)
      
      shicao[, LHS := LHS - LHS_remainder + 1]
      shicao[, RHS := RHS - RHS_remainder]
      
      shicao[, LHS + RHS]
      
      shicao[, LHS_remainder := 0]
      shicao[, RHS_remainder := 0]
      
      # round 2
      
      N = shicao[, LHS + RHS]
      
      shicao = data.table(
        LHS = 8:(N-8),
        RHS = (N-8):8
      )
      
      sel = sample(1:shicao[, .N], 1)
      shicao = shicao[sel, ]
      
      shicao[, LHS := LHS - 1]
      shicao[, LHS_remainder := LHS %% 4]
      shicao[LHS_remainder == 0, LHS_remainder := 4]
      shicao[, LHS_remainder := LHS_remainder + 1]
      
      shicao[, RHS_remainder := RHS %% 4]
      shicao[RHS_remainder == 0, RHS_remainder := 4]
      
      shicao[, LHS_remainder + RHS_remainder] %in% c(4, 8)
      
      shicao[, LHS := LHS - LHS_remainder + 1]
      shicao[, RHS := RHS - RHS_remainder]
      
      shicao[, LHS + RHS]
      
      shicao[, LHS_remainder := 0]
      shicao[, RHS_remainder := 0]
      
      # round 3
      
      N = shicao[, LHS + RHS]
      
      shicao = data.table(
        LHS = 4:(N-4),
        RHS = (N-4):4
      )
      
      sel = sample(1:shicao[, .N], 1)
      shicao = shicao[sel, ]
      
      shicao[, LHS := LHS - 1]
      shicao[, LHS_remainder := LHS %% 4]
      shicao[LHS_remainder == 0, LHS_remainder := 4]
      shicao[, LHS_remainder := LHS_remainder + 1]
      
      shicao[, RHS_remainder := RHS %% 4]
      shicao[RHS_remainder == 0, RHS_remainder := 4]
      
      shicao[, LHS_remainder + RHS_remainder] %in% c(4, 8)
      
      shicao[, LHS := LHS - LHS_remainder + 1]
      shicao[, RHS := RHS - RHS_remainder]
      
      shicao[, LHS + RHS]
      
      shicao[, LHS_remainder := 0]
      shicao[, RHS_remainder := 0]
      
      # end
      
      N = shicao[, LHS + RHS]
      
      return(N / 4)
    }
    
    jiegua = data.table()
    
    for( i in 1:6 ) {
      jiegua = rbind(jiegua, data.table( order = i, yao = zhanbu() ))
    }
    
    jiegua[yao %in% c(7, 9), YAO := '阳']
    jiegua[yao %in% c(6, 8), YAO := '阴']
    
    jiegua = str_c(jiegua$YAO, collapse = '')
    
    guaxiang = c(
      '阳阳阳阳阳阳', # 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
      '阳阳阴阴阴阳', # 41
      '阳阴阴阴阳阳', # 42
      '阳阳阳阳阳阴', # 43
      '阴阳阳阳阳阳', # 44
      '阴阴阴阳阳阴', # 45
      '阴阳阳阴阴阴', # 46
      '阴阳阴阳阳阴', # 47
      '阴阳阳阴阳阴', # 48
      '阳阴阳阳阳阴', # 49
      '阴阳阳阳阴阳', # 50
      '阳阴阴阳阴阴', # 51
      '阴阴阳阴阴阳', # 52
      '阴阴阳阴阳阳', # 53
      '阳阳阴阳阴阴', # 54
      '阳阴阳阳阴阴', # 55
      '阴阴阳阳阴阳', # 56
      '阴阳阳阴阳阳', # 57
      '阳阳阴阳阳阴', # 58
      '阴阳阴阴阳阳', # 59
      '阳阳阴阴阳阴', # 60
      '阳阳阴阴阳阳', # 61
      '阴阴阳阳阴阴', # 62
      '阳阴阳阴阳阴', # 63
      '阴阳阴阳阴阳'  # 64
    )
    
    guaming = c(
      '乾为天',   # 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
      '山泽损',   # 41
      '风雷益',   # 42
      '泽天夬',   # 43
      '天风姤',   # 44
      '泽地萃',   # 45
      '地风升',   # 46
      '泽水困',   # 47
      '水风井',   # 48
      '泽火革',   # 49
      '火风鼎',   # 50
      '震为雷',   # 51
      '艮为山',   # 52
      '风山渐',   # 53
      '雷泽归妹', # 54
      '雷火丰',   # 55
      '火山旅',   # 56
      '巽为风',   # 57
      '兑为泽',   # 58
      '风水涣',   # 59
      '水泽节',   # 60
      '风泽中孚', # 61
      '雷山小过', # 62
      '水火既济', # 63
      '火水未济'  # 64
    )
    
    guabiao = data.table( guaxiang, guaming )
    
    print(str_glue(
      '卦象:', guabiao[guaxiang == jiegua, guaxiang], '\n',
      '卦名:', guabiao[guaxiang == jiegua, guaming]
    ))
    
    


  • 哈哈哈哈哈哈厉害


登录后回复