365体育网投R语言真是博大精深。方法。

R语言真是博大精深

  • 开创集合

方法一

Acf(gold[,2], type = "correlation",lag.max = 100)
Acf(gold[,2], type = "partial")

方法二

library(ggfortify)
autoplot(acf(gold[,2], plot = FALSE))
    /**
     *  @param mixed $value
     *  @return \Illuminate\Support\Collection
     *  辅助函数 collect 创建 Collection 对象
     */
     Collection function collect($value = null);
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    /**
     *  @param mixed $value
     *  @return \Illuminate\Support\Collection
     *  采用 new 关键字创建Collection集合对象
     */
    new \Illuminate\Support\Collection($value);

方法三

bacf <- acf(gold[, 2], plot = FALSE)
bacfdf <- with(bacf, data.frame(lag, acf))
ggplot(data = bacfdf, mapping = aes(x = lag, y = acf)) +
    geom_segment(mapping = aes(xend = lag, yend = 0)) +
    geom_hline(aes(yintercept = 0.05), linetype = 2, color = 'darkblue')
# pacf
bpacf <- pacf(gold[, 2], plot = FALSE)
bpacf <- with(bpacf, data.frame(lag, acf))
ggplot(data = bpacf, mapping = aes(x = lag, y = acf)) +
    geom_segment(mapping = aes(xend = lag, yend = 0)) +
    geom_hline(aes(yintercept = 0.05), linetype = 2, color = 'darkblue')
  • all 方法

方法四

library("forecast")
forecast::ggtsdisplay(gold[,2],lag.max=30)

方法五

ggAcf(gold[,2])
ggPacf(gold[,2])
    /**
     * @return array
     * 得到当前Collection的值 $this->items
     */
     public function all();
  • map 方法

# 参看 array_map() 函数
$collection = collect(array('hello'=>"miss",'php'=>"mr",'or','golang'));
# 遍历集合数组元素(返回插入集合数值)
$coll = $collection->map(function ($value,$key){
    # $value 值 $key 键
    return "a12";
});
dump($coll); //> collect(array('hello'=>"a12",'php'=>"a12","a12","a12"))
  • reject 方法

# 过滤集合中元素参看 array_filter() 方法
$collection = collect(array('hello'=>"miss",'php'=>"mr",'or','golang'));
# 遍历集合数组元素(返回插入集合数值)
$coll = $collection->reject(function ($value,$key){
      # $value 值 $key 键
      # 返回值 bool:true 过滤该值
    if( $key === 'php' ){
        return true;
    }
    return false;
});
dump($coll); //> collect(array('hello'=>"miss,'or','golang'))
  • avg 方法

$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->avg(); //> 不推荐
dump($collection); //> 1.25 = (2+3)/4
$collection = collect(array(1,3,5,7,9))->avg();
dump($collection); //> 5 = (1+3+5+7+9)/5
$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->avg('php');
dump($collection); //> 22 = (12+32)/2
  • max 方法

# max 推荐纯数值集合
$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->max();  //> 不推荐
dump($collection); //> 3
$collection = collect(array(1,3,5,7,9))->max();
dump($collection); //> 9
$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->max('php');
dump($collection); //> 32
  • min 方法

# min 推荐纯数值集合
$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->min(); //> 不推荐
dump($collection); //> miss
$collection = collect(array(1,3,5,7,9))->min();
dump($collection); //> 1
$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->min('php');
dump($collection); //> 12
  • sum 方法

$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->sum();
dump($collection); //> 5
$collection = collect(array(1,3,5,7,9))->sum();
dump($collection); //> 25
$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->sum('php');
dump($collection); //> 44
  • tap 方法

    /**
     * Pass the collection to the given callback and then return it.
     *
     * @param  callable  $callback
     * @return $this
     */
    public function tap(callable $callback)
    {
        $callback(new static($this->items));

        return $this;
    }
    $collection = collect(array('hello'=>"miss",'php'=>2,'or',3));
    $collection->tap(function($coll){
        dump($coll); //> collect(array('hello'=>"miss",'php'=>2,'or',3))
    });
  • toArray 方法

    /**
     * Get the collection of items as a plain array.
     *
     * @return array
     */
    public function toArray()
    {
        return array_map(function ($value) {
            return $value instanceof Arrayable ? $value->toArray() : $value;
        }, $this->items);
    }
  • toJson 方法

    /**
     * Get the collection of items as JSON.
     *
     * @param  int  $options
     * @return string
     */
    public function toJson($options = 0)
    {
        return json_encode($this->jsonSerialize(), $options);
    }
    /**
     * Convert the object into something JSON serializable.
     *
     * @return array
     */
    public function jsonSerialize()
    {
        return array_map(function ($value) {
            if ($value instanceof JsonSerializable) {
                return $value->jsonSerialize();
            } elseif ($value instanceof Jsonable) {
                return json_decode($value->toJson(), true);
            } elseif ($value instanceof Arrayable) {
                return $value->toArray();
            } else {
                return $value;
            }
        }, $this->items);
    }
  • zip 方法

    /**
     * Zip the collection together with one or more arrays.
     *
     * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]);
     *      => [[1, 4], [2, 5], [3, 6]]
     *
     * @param  mixed ...$items
     * @return static
     */
    public function zip($items)
    {
        $arrayableItems = array_map(function ($items) {
            return $this->getArrayableItems($items);
        }, func_get_args());

        $params = array_merge([function () {
            return new static(func_get_args());
        }, $this->items], $arrayableItems);

        return new static(call_user_func_array('array_map', $params));
    }
  • median 方法

# 获取给定集合的中间值(排序后的中间值)
# 集合是奇数 获取中间数值
# 集合是偶数 获取中间两个数的平均值
$collection = collect(array('hello'=>34,'php'=>2,'we'=>32,'ed'=>54,'wd'=>3,43));
dump($collection->median());  //> 33 = (34+32)/2
$collection = collect(array('hello'=>34,'php'=>2,'we'=>32,'ed'=>54,'wd'=>3));
dump($collection->median()); //> 32
$collection = collect(array(array(12,11,'php'=>43),array(12,1,'php'=>23),array('php'=>12,3)));
dump($collection->median('php')); //> 23
  • mode 方法

# 返回出现次数最多的数
$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array('php'=>12,3)));
dump($collection->mode('php')); //> [13]
$collection = collect(array('hello'=>34,'php'=>2,'we'=>34,'ed'=>34,'wd'=>3,2,2));
dump($collection->mode());  //> [34,2]
$collection = collect(array(12,3,234,21,32,123,1,1,2,2));
dump($collection->mode());  //> [1,2]
  • each 方法

    /**
     * Execute a callback over each item.
     *
     * @param  callable  $callback
     * @return $this
     */
    public function each(callable $callback)
    {
        foreach ($this->items as $key => $item) {
            if ($callback($item, $key) === false) {
                break;
            }
        }

        return $this;
    }
$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array('php'=>12,3)));
$collection->each(function ($value,$key){
    # $value 值 $key 键
    dump($value);
    dump($key);
});
  • every 方法

# 检查集合中每一个元素是否符合条件
# 源码参考
    /**
     * Determine if all items in the collection pass the given test.
     *
     * @param  string|callable  $key
     * @param  mixed  $operator
     * @param  mixed  $value
     * @return bool
     */
    public function every($key, $operator = null, $value = null)
    {
        if (func_num_args() == 1) {
            $callback = $this->valueRetriever($key);

            foreach ($this->items as $k => $v) {
                if (! $callback($v, $k)) {
                    return false;
                }
            }

            return true;
        }

        if (func_num_args() == 2) {
            $value = $operator;

            $operator = '=';
        }

        return $this->every($this->operatorForWhere($key, $operator, $value));
    }
# 使用示例
$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array('php'=>12,3)));
$result = $collection->every(function($v,$k){
    # $v 值 $k 键
    # 条件判断 
    # return false; 比较失败终止比较返回false
    # return true; 本次比较通过,进行下次比较
    return true;
});
dump($result);    //> true
dump($collection->every('php','>',12)); //> false
# php键 是否存在集合里(集合里的每一个数组中)
dump($collection->every('php')); //> true 
dump(collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array(3)))->every('php')); //> false
# 支持比较符 null(=) = == != <> > < >= <= === !==
# 更多用法 - 参看源码
  • when 方法

# 源码 - when 方法使用很简单这里不详细举例了
    /**
     * Apply the callback if the value is truthy.
     *
     * @param  bool  $value            # 判断条件
     * @param  callable  $callback
     * @param  callable  $default
     * @return mixed
     */
    public function when($value, callable $callback, callable $default = null)
    {
        if ($value) {
            return $callback($this);
        } elseif ($default) {
            return $default($this);
        }

        return $this;
    }
  • unless 方法

# 源码 - 参考when方法
    /**
     * Apply the callback if the value is falsy.
     *
     * @param  bool  $value            # 判断条件
     * @param  callable  $callback
     * @param  callable  $default
     * @return mixed
     */
    public function unless($value, callable $callback, callable $default = null)
    {
        return $this->when(! $value, $callback, $default);
    }
  • where 方法

# 过滤掉其他数据 刷选
    /**
     * Filter items by the given key value pair.
     *
     * @param  string  $key
     * @param  mixed  $operator
     * @param  mixed  $value
     * @return static
     */
    public function where($key, $operator, $value = null)
    {
        if (func_num_args() == 2) {
            $value = $operator;

            $operator = '=';
        }

        return $this->filter($this->operatorForWhere($key, $operator, $value));
    }
# 使用示例
$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array(3)));
dump($collection->where('php','=',13)); //> collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13)))
# 支持比较符 null(=) = == != <> > < >= <= === !==
  • whereStrict 方法

# 参考where方法 严格等于 筛选
    /**
     * Filter items by the given key value pair using strict comparison.
     *
     * @param  string  $key
     * @param  mixed  $value
     * @return static
     */
    public function whereStrict($key, $value)
    {
        return $this->where($key, '===', $value);
    }
  • whereIn 方法

# 源码 - 给定集合键(过滤集合)是否在另一个数组或集合里存在
    /**
     * Filter items by the given key value pair.
     *
     * @param  string  $key       # 给定集合里的键
     * @param  mixed  $values # 另一个集合或数组
     * @param  bool  $strict       # 是否开启严格比较
     * @return static
     */
    public function whereIn($key, $values, $strict = false)
    {
        $values = $this->getArrayableItems($values);

        return $this->filter(function ($item) use ($key, $values, $strict) {
            return in_array(data_get($item, $key), $values, $strict);
        });
    }
  • whereInStrict 方法

# 严格过滤 - 参看whereIn方法
    /**
     * Filter items by the given key value pair using strict comparison.
     *
     * @param  string  $key
     * @param  mixed  $values
     * @return static
     */
    public function whereInStrict($key, $values)
    {
        return $this->whereIn($key, $values, true);
    }
  • whereNotIn 方法

# 该方法正好和whereIn用法相反
    /**
     * Filter items by the given key value pair.
     *
     * @param  string  $key
     * @param  mixed  $values
     * @param  bool  $strict
     * @return static
     */
    public function whereNotIn($key, $values, $strict = false)
    {
        $values = $this->getArrayableItems($values);

        return $this->reject(function ($item) use ($key, $values, $strict) {
            return in_array(data_get($item, $key), $values, $strict);
        });
    }
  • whereNotInStrict 方法

# 该方法和whereInStrict用法相反
    /**
     * Filter items by the given key value pair using strict comparison.
     *
     * @param  string  $key
     * @param  mixed  $values
     * @return static
     */
    public function whereNotInStrict($key, $values)
    {
        return $this->whereNotIn($key, $values, true);
    }

相关文章