【问题标题】:create a joint frequency distribution table in r在 r 中创建联合频率分布表
【发布时间】:2021-09-22 09:11:50
【问题描述】:

我试图在 R 中创建两个连续变量之间的联合频率分布表,电价和可再生能源生产,对于某些范围,例如使用 10 个 20 欧元的价格范围和 10 个 10.000 兆瓦时的可再生电力范围生产。 我的数据如下所示:

structure(list(DATE = structure(c(18628, 18629, 18630, 18631, 
18632, 18633, 18634, 18635, 18636, 18637, 18638, 18639, 18640, 
18641, 18642, 18643, 18644, 18645, 18646, 18647), class = "Date"), 
    price = c(45.5804166666667, 47.11125, 43.4683333333333, 47.4579166666667, 
    50.8945833333333, 52.84375, 44.64375, 55.7233333333333, 51.64125, 
    51.9604166666667, 51.6475, 52.4683333333333, 67.7625, 46.1591666666667, 
    63.68875, 50.955, 53.9758333333333, 66.94625, 71.73, 63.4458333333333
    ), RES = c(23534.08, 21286.57, 42159.53, 45660.36, 36967.42, 
    33727.61, 45660.92, 59931.54, 48146.49, 48424.06, 60207.69, 
    45786.85, 38909.96, 49843.98, 43003.73, 41491, 41655.27, 
    42133.29, 31207.87, 36045.62)), row.names = c(NA, 20L), class = "data.frame") 

我无法构建这种具有某些频率包的表,有人知道如何做到这一点吗?提前谢谢你。

【问题讨论】:

    标签: r distribution frequency


    【解决方案1】:

    数据:

    df <- structure(list(DATE = structure(c(18628, 18629, 18630, 18631, 
                                            18632, 18633, 18634, 18635, 18636, 18637, 18638, 18639, 18640, 
                                            18641, 18642, 18643, 18644, 18645, 18646, 18647), class = "Date"), 
                         price = c(45.5804166666667, 47.11125, 43.4683333333333, 47.4579166666667, 
                                   50.8945833333333, 52.84375, 44.64375, 55.7233333333333, 51.64125, 
                                   51.9604166666667, 51.6475, 52.4683333333333, 67.7625, 46.1591666666667, 
                                   63.68875, 50.955, 53.9758333333333, 66.94625, 71.73, 63.4458333333333
                         ), RES = c(23534.08, 21286.57, 42159.53, 45660.36, 36967.42, 
                                    33727.61, 45660.92, 59931.54, 48146.49, 48424.06, 60207.69, 
                                    45786.85, 38909.96, 49843.98, 43003.73, 41491, 41655.27, 
                                    42133.29, 31207.87, 36045.62)), row.names = c(NA, 20L), class = "data.frame") 
    

    代码:

    # check min and max of your respective vectors price and RES
    
    # use cut to create ranges 
    df$priceRanges <- cut(df$price, seq(40, 80, by = 10))
    df$RESRanges <- cut(df$RES, seq(20000, 70000, by = 10000))
    
    # table the created variables with relative frequencies
    prop.table(table(df[, c("priceRanges", "RESRanges")]))
    
    ## 1
    # export as txt with rownames to working directory
    new <- prop.table(table(df[, c("priceRanges", "RESRanges")]))
    write.table(new, file = "new.txt", sep = ",", quote = FALSE, row.names = TRUE)
    
    ## 2
    # export as csv ...
    write.csv2(new, file = "new.csv", quote = FALSE, row.names = TRUE)
    

    【讨论】:

    • 感谢您的快速回复!输出可以以百分比形式形成吗?例如,为了查看在某个可再生电力生产水平上电价的频率百分比是多少?
    • 试试prop.table(table(df[, c("priceRanges", "RESRanges")]))
    • 非常感谢!你知道打印这个表格的方法吗?我使用了 fwrite 函数,但它垂直打印所有频率。
    • 出于什么目的?乳胶?
    • 我不熟悉 Latex。让 R 生成一个“联合”表更方便,保持代码输出中的行和列,而不是让频率全部垂直。
    猜你喜欢
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2018-12-13
    • 2013-02-09
    • 2022-01-03
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    相关资源
    最近更新 更多