zoukankan      html  css  js  c++  java
  • R语言学习笔记(二十四):plyr包的用法

    plyr 这个包,提供了一组规范的数据结构转换形式。

    Input/Output list data frame array
    list        |	llply()	| ldply()     |	laply()
    

    data frame | dlply() | ddply() | daply()
    array | alply() | adply() | aaply()

    一个简单的例子

    • 普通做法
    iris.set <- iris[iris$Species == "setosa", -5]
    iris.versi <- iris[iris$Species == "versicolor", -5]
    iris.virg <- iris[iris$Species == "virginica", -5]
    ## Apply
    mean.set <- colMeans(iris.set)
    mean.versi <- colMeans(iris.versi)
    mean.virg <- colMeans(iris.virg)
    ## Combine
    mean.iris <- rbind(mean.set, mean.versi, mean.virg)
    rownames(mean.iris) <- c("setosa", "versicolor", "virginica")
    mean.iris
               Sepal.Length Sepal.Width Petal.Length Petal.Width
    setosa            5.006       3.428        1.462       0.246
    versicolor        5.936       2.770        4.260       1.326
    virginica         6.588       2.974        5.552       2.026
    
    • plyr
    library(plyr)
    ddply(iris, .variables = "Species", .fun = function(df_sub){
        colMeans(df_sub[, -5])
    })
         Species Sepal.Length Sepal.Width Petal.Length Petal.Width
    1     setosa        5.006       3.428        1.462       0.246
    2 versicolor        5.936       2.770        4.260       1.326
    3  virginica        6.588       2.974        5.552       2.026
    
    ddply(iris, .variables = "Species", .fun = function(df_sub) {
        model <- lm(Petal.Width ~ Petal.Length, data = df_sub)
        return(c(model$coefficients, R2 = summary(model)$r.squared))
    })
         Species (Intercept) Petal.Length        R2
    1     setosa -0.04822033    0.2012451 0.1099785
    2 versicolor -0.08428835    0.3310536 0.6188467
    3  virginica  1.13603130    0.1602970 0.1037537
    
  • 相关阅读:
    docker-compose.yml(2)
    docker-compose.yml(3)
    docker-compose.yml(4)
    生产者&消费者.py
    工作机制.py
    2.5 webpack 进阶
    2.3 Gulp
    2.4 webpack + gulp 构建完整前端工作流
    2.2 webpack
    2.1 前端工程化概述
  • 原文地址:https://www.cnblogs.com/xihehe/p/8313178.html
Copyright © 2011-2022 走看看