R中的DataTable,将具有特定值类别的行格式化为百分比 [英] DataTable in R, formatting rows with specific value category to a percentage

查看:146
本文介绍了R中的DataTable,将具有特定值类别的行格式化为百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个datatable,我的目标是更改任何包含MONTH =Percent Change:的行到百分比:

If I have a datatable and my goal is to change any rows containing MONTH="Percent Change:" to percentage:

             MONTH YEAR                    Client      Revenue  Metric 1          Metric 2         Metric 3
1:          MTD: 1 2015                  Client A 255999.33000 5.251913e+07     3.476303e+07   0.66191181
2:          MTD: 1 2016                  Client A 393450.17000 6.676211e+07     3.557979e+07   0.53293384
3: Percent Change: 2016                  Client A     53.69187 2.711961e+01     2.349502e+00 -19.48567206
4:          MTD: 1 2016                  Client B 178793.62000 1.339837e+09     2.527131e+07   0.01886148
5: Percent Change: 2016                  Client B           NA           NA               NA           NA
6:          MTD: 1 2015                  Client C  98492.19000 1.535520e+08     2.213594e+07   0.14415924
    Metric 4
1:  7.364126
2: 11.058249
3: 50.163774
4:  7.074964
5:        NA
6:  4.449424

datatable(df) %>%
  formatPercentage(df[which(df$MONTH== "Percent Change:"),],2)

如何仅将数据库中的百分比更改行格式化为百分比?

How do I format only the Percent Change Rows in a datatable to percentage?

预期输出:

             MONTH YEAR                    Client      Revenue  Metric 1          Metric 2         Metric 3
1:          MTD: 1 2015                  Client A 255999.33000 5.251913e+07     3.476303e+07   0.66191181
2:          MTD: 1 2016                  Client A 393450.17000 6.676211e+07     3.557979e+07   0.53293384
3: Percent Change: 2016                  Client A     53.69187% 2.711961e+01%     2.349502e+00% -19.48567206%
4:          MTD: 1 2016                  Client B 178793.62000 1.339837e+09     2.527131e+07   0.01886148
5: Percent Change: 2016                  Client B           NA           NA               NA           NA
6:          MTD: 1 2015                  Client C  98492.19000 1.535520e+08     2.213594e+07   0.14415924
    Metric 4
1:  7.364126
2: 11.058249
3: 50.163774%
4:  7.074964
5:        NA
6:  4.449424

dput: 

function (x, file = "", control = c("keepNA", "keepInteger", 
    "showAttributes")) 
{
    if (is.character(file)) 
        if (nzchar(file)) {
            file <- file(file, "wt")
            on.exit(close(file))
        }
        else file <- stdout()
    opts <- .deparseOpts(control)
    if (isS4(x)) {
        clx <- class(x)
        cat("new(\"", clx, "\"\n", file = file, sep = "")
        for (n in methods::.slotNames(clx)) {
            cat("    ,", n, "= ", file = file)
            dput(methods::slot(x, n), file = file, control = control)
        }
        cat(")\n", file = file)
        invisible()
    }
    else .Internal(dput(x, file, opts))
}
<bytecode: 0x000000003031c860>
<environment: namespace:base>

当我尝试并运行我的datatable时出现的错误是:

The error that comes up when I try and run my datatable is this:

Error in names[name] : invalid subscript type 'list'


推荐答案

我们可以将第4到8列转换为字符类。然后,使用 i 中的逻辑条件,我们循环第4到8列,粘贴 并将(:= )分配回列。

We can convert the columns 4 to 8 as character class. Then, using the logical condition in i, we loop over the columns 4 to 8, paste the % and assign (:=) it back to the columns.

library(data.table)
setDT(df)[, (4:8) := lapply(.SD, as.character), .SDcols= 4:8]
df[MONTH=="Percent Change:", (4:8) := 
    lapply(.SD, function(x) paste0(x[!is.na(x)],"%")), .SDcols=4:8]

这篇关于R中的DataTable,将具有特定值类别的行格式化为百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆