【问题标题】:R Swap values in columns checking alphabetical orderR在检查字母顺序的列中交换值
【发布时间】:2020-09-23 11:34:16
【问题描述】:

我正在使用 R 中的一个 HUGE data.frame。我的数据集遵循以下模式:

+--------+------+------+------+------+ | | Col1 | Col2 | Col3 | Col4 | +--------+------+------+------+------+ | 1号线 | 43 |一个 |乙 | 56 | |线2 | 103 | c | d | 85 | | 3号线 | 7 | F | E | 115 | | 4号线 | 8 |克 | h | 0 | +--------+------+------+------+------+

我必须做到以下几点:


For each row:
    If Col2 > Col3 (check alphabetical order of the values)
        Swap values of Col 2 and Col 3

我需要以下结果:

+--------+------+------+------+------+ | | Col1 | Col2 | Col3 | Col4 | +--------+------+------+------+------+ | 1号线 | 43 |一个 |乙 | 56 | |线2 | 103 | c | d | 85 | | 3号线 | 7 | E | F | 115 | | 4号线 | 8 |克 | h | 0 | +--------+------+------+------+------+

我为此编写了一个 for 循环,但这需要很长时间!有没有更有效的方法来使用 R 来做到这一点?

谢谢!

【问题讨论】:

    标签: r dataframe


    【解决方案1】:

    这可能不是最有效或最快的解决方案,但在我的机器上使用 1E8 行时花费不到 90 秒:

    set.seed(123)
    nr <- 1E8
    df <- data.frame(
        Col1 = sample(1:200, nr, replace = TRUE),
        Col2 = sample(c(LETTERS, letters), nr, replace=TRUE),
        Col3 = sample(c(LETTERS, letters), nr, replace=TRUE),
        Col4 = sample(1:200, nr, replace = TRUE)
    )
    
    library(data.table)
    setDT(df)
    df[]
    #>            Col1 Col2 Col3 Col4
    #>         1:  159    P    L   70
    #>         2:  179    L    o  159
    #>         3:   14    w    O  168
    #>         4:  195    K    H  193
    #>         5:  170    r    X  116
    #>        ---                    
    #>  99999996:  117    O    F  163
    #>  99999997:   82    Q    q  179
    #>  99999998:  128    t    U   60
    #>  99999999:   40    X    o   79
    #> 100000000:  185    E    o  133
    system.time({
    df[, `:=` (Col2=fifelse(Col2 < Col3, Col2, Col3),
               Col3=fifelse(Col2 < Col3, Col3, Col2))]
    })
    #>    user  system elapsed 
    #>  17.326   0.368  17.694
    df[]
    #>            Col1 Col2 Col3 Col4
    #>         1:  159    L    P   70
    #>         2:  179    L    o  159
    #>         3:   14    O    w  168
    #>         4:  195    H    K  193
    #>         5:  170    r    X  116
    #>        ---                    
    #>  99999996:  117    F    O  163
    #>  99999997:   82    q    Q  179
    #>  99999998:  128    t    U   60
    #>  99999999:   40    o    X   79
    #> 100000000:  185    E    o  133
    

    reprex package (v0.3.0) 于 2020 年 6 月 4 日创建

    【讨论】:

    • 谢谢。我在 Mac 中安装 data.table 时遇到问题。但是问题解决了。
    【解决方案2】:

    你可以使用变换功能。

    > df
          col1 col2 col3 col4
    Line1   48    a    b   56
    Line2  103    c    d   85
    Line3    7    f    e  115
    Line4    8    g    h    0
    
    > df <- transform(df, col2 = pmin(col2, col3), col3=pmax(col2, col3))
    
    > df
          col1 col2 col3 col4
    Line1   48    a    b   56
    Line2  103    c    d   85
    Line3    7    e    f  115
    Line4    8    g    h    0
    

    【讨论】:

    • 超级简单!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    相关资源
    最近更新 更多