【问题标题】:How to re-sort information within a table/ compress information within a table如何重新排序表中的信息/压缩表中的信息
【发布时间】:2016-12-09 15:20:31
【问题描述】:

我正在使用 RStudio

所以我想知道如何使用下面输入的数据:

当前表:

Value.  Biotop  
1. Forest Coniferous
2. Soil Type
3. Urban Green Areas
4. Buildings/Urban Area
5. Forest Coniferous
6. Forest Coniferous
7. Forest Coniferous
8. Forest Coniferous
9. Forest Coniferous
10. Forest Coniferous
11. Forest Coniferous
12. Forest Coniferous
13. Soil Type
14. Forest Coniferous
15. Forest Coniferous
16. Forest Coniferous
17. Forest Coniferous
18. Soil Type
19. Soil Type
20. Soil Type
21. Soil Type
22. Soil Type
23. Soil Type
24. Soil Type
25. Urban Green Areas
26. Urban Green Areas
27. Urban Green Areas
28. Urban Green Areas
29. Urban Green Areas
30. Urban Green Areas
31. Urban Green Areas

将我的数据重新排序为类似这样的内容:

未来表:

Value. Biotop
.1. Forest Coniferous
.2. Soil type.
.3. Urban Green Area
.4. Buildings/Urban Area
.5. -- 12. Forest Coniferous
.13. Soil Type
.14. -- 17. Forest Coniferous
.18. -- 24. Soil Type 
.25. -- 31. Urban Green Areas

抱歉,缺乏良好的可视化效果,我(您可能会看到)对此很陌生。无论如何,它们都是为了描绘表格。

【问题讨论】:

    标签: r dataframe aggregate


    【解决方案1】:

    使用 data.table 包,您可以在单行中实现此目的(有点):

    library(data.table)
    setDT(dt)[, .(Value = paste(unique(range(Value)), collapse = "-"), 
           Biotop = Biotop[1]), by = rleid(Biotop)][, rleid := NULL][]
    

    返回

    #   Value               Biotop
    #1:     1    Forest Coniferous
    #2:     2            Soil Type
    #3:     3    Urban Green Areas
    #4:     4 Buildings/Urban Area
    #5:  5-12    Forest Coniferous
    #6:    13            Soil Type
    #7: 14-17    Forest Coniferous
    #8: 18-24            Soil Type
    #9: 25-31    Urban Green Areas
    

    说明

    setDT() 确保 dt 属于 data.table 类(如果它还没有的话)。然后,我们为Biotop 的连续条纹创建一个索引,并按此索引进行分组。对于每个组,获取最低和最高的Value(使用range() 函数)并粘贴到14-17 形式的字符串中,例如为了避免像13-13 这样的情况,我们使用unique 将字符串折叠为13。最后,我们删除了仅用于分组的索引。

    请注意,这假定Value 在每个组中都是有序的,因此我们可以将最低和最高的Value 用于第一个和最后一个条目。

    数据

    library(data.table)
    dt <- fread("Value.  Biotop  
                1. Forest Coniferous
                2. Soil Type
                3. Urban Green Areas
                4. Buildings/Urban Area
                5. Forest Coniferous
                6. Forest Coniferous
                7. Forest Coniferous
                8. Forest Coniferous
                9. Forest Coniferous
                10. Forest Coniferous
                11. Forest Coniferous
                12. Forest Coniferous
                13. Soil Type
                14. Forest Coniferous
                15. Forest Coniferous
                16. Forest Coniferous
                17. Forest Coniferous
                18. Soil Type
                19. Soil Type
                20. Soil Type
                21. Soil Type
                22. Soil Type
                23. Soil Type
                24. Soil Type
                25. Urban Green Areas
                26. Urban Green Areas
                27. Urban Green Areas
                28. Urban Green Areas
                29. Urban Green Areas
                30. Urban Green Areas
                31. Urban Green Areas", sep =".")
    

    【讨论】:

    • 谢谢,虽然当我按照你的建议运行它时,这是我得到的错误消息: [.tbl_df(df, , .(Value = paste(unique(range(Value))), 中的错误:未使用的参数(by = rleid(Biotop))
    • 我在代码中添加了setDT(),以确保它是data.table
    • @Nedim:很抱歉,我不知道您以哪种形式存储数据,因为您的 Q 中缺少此信息。(下次请添加 @987654339 的输出@ 或至少 str(...) 到您的 Q)。从错误消息中我可以看到您已将数据存储为tibble,需要将其转换为data.table(使用setDT())以使单线工作。
    • 这次效果很好,以后我会尽量详细说明我的问题的条件。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-06-27
    • 2018-07-01
    • 1970-01-01
    • 2016-03-14
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多