【问题标题】:R - kableExtra - footnote() : Difficulty to code the for loopR - kableExtra - 脚注():难以编写 for 循环
【发布时间】:2019-09-29 18:07:02
【问题描述】:

此操作的目的是如果计算到“JO_ratio_tranches”表中的时隙对应的比率对象小于5%(在“voy_tranches”表中显示为红色表)。

> dput(voy_tranches)
structure(list(tranches = c(".1_JO[4h_7h[", ".2_JO[7h_9h[", ".3_JO[9h_12h[", 
".4_JO[12h_15h30[", ".5_JO[15h30_18h[", ".6_JO[18h_20h[", ".7_JO[20h_22h[", 
".8_JO[22h_4h["), voy = c(2207, 13051, 14049, 19055, 20094, 7618, 
2544, 704), montees = c(847, 6912, 7701, 11008, 10820, 4082, 1286, 352), dist = c(2.61, 1.89, 1.82, 1.73, 1.86, 1.87, 1.98, 2)), row.names = c(NA, -8L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000000121ef0>)

> dput(JO_ratio_tranches)
structure(list(chem = c("257-ROG1D-NOR4-THOM-LIED3-DVA3", "302-DVA3-DVI4-BOR7-PAIX-ROG1A"), dir = c("R", "A"), `.1_JO[4h_7h[_txcouv` = c(6.7, 6.5), `.2_JO[7h_9h[_txcouv` = c(3.9, 7), `.3_JO[9h_12h[_txcouv` = c(4.5, 5.7), `.4_JO[12h_15h30[_txcouv` = c(5.8, 7), `.5_JO[15h30_18h[_txcouv` = c(3.7, 6.3), `.6_JO[18h_20h[_txcouv` = c(4.1, 5.3), `.7_JO[20h_22h[_txcouv` = c(3.3, 3.9), `.8_JO[22h_4h[_txcouv` = c(2.9, 3.6)), row.names = c(NA, -2L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000000000121ef0>, sorted = "chem")

我正在尝试将此代码矢量化为一个 for 循环,但我总是收到一条错误消息,我不知道如何解决它!

ratio_1 <- JO_ratio_tranches[`.1_JO[4h_7h[_txcouv` < 5, `.1_JO[4h_7h[_txcouv`]
if(!is_empty(ratio_1)){voy_tranches[1,1] <- paste(voy_tranches[1,1], footnote_marker_symbol(1))}

ratio_2 <- JO_ratio_tranches[`.2_JO[7h_9h[_txcouv` < 5, `.2_JO[7h_9h[_txcouv`]
if(!is_empty(ratio_2)){voy_tranches[2,1] <- paste(voy_tranches[2,1], footnote_marker_symbol(1))}

ratio_3 <- JO_ratio_tranches[`.3_JO[9h_12h[_txcouv` < 5, `.3_JO[9h_12h[_txcouv`]
if(!is_empty(ratio_3)){voy_tranches[3,1] <- paste(voy_tranches[3,1], footnote_marker_symbol(1))}

ratio_4 <- JO_ratio_tranches[`.4_JO[12h_15h30[_txcouv` < 5, `.4_JO[12h_15h30[_txcouv`]
if(!is_empty(ratio_4)){voy_tranches[4,1] <- paste(voy_tranches[4,1], footnote_marker_symbol(1))}

ratio_5 <- JO_ratio_tranches[`.5_JO[15h30_18h[_txcouv` < 5, `.5_JO[15h30_18h[_txcouv`]
if(!is_empty(ratio_5)){voy_tranches[5,1] <- paste(voy_tranches[5,1], footnote_marker_symbol(1))}

ratio_6 <- JO_ratio_tranches[`.6_JO[18h_20h[_txcouv` < 5, `.6_JO[18h_20h[_txcouv`]
if(!is_empty(ratio_6)){voy_tranches[6,1] <- paste(voy_tranches[6,1], footnote_marker_symbol(1))}

ratio_7 <- JO_ratio_tranches[`.7_JO[20h_22h[_txcouv` < 5, `.7_JO[20h_22h[_txcouv`]
if(!is_empty(ratio_7)){voy_tranches[7,1] <- paste(voy_tranches[7,1], footnote_marker_symbol(1))}

ratio_8 <- JO_ratio_tranches[`.8_JO[22h_4h[_txcouv` < 5, `.8_JO[22h_4h[_txcouv`]
if(!is_empty(ratio_8)){voy_tranches[8,1] <- paste(voy_tranches[8,1], footnote_marker_symbol(1))}

好的!

for(i in JO_ratio_tranches[, grep("txcouv", names(JO_ratio_tranches))]){
ratio[[i]] <- JO_ratio_tranches[JO_ratio_tranches[[i]] < 5, ]
if(!is_empty(ratio[[i]])){voy_tranches[i, 1] <- paste(voy_tranches[i, 1], footnote_marker_symbol(1))}
}

eval(expr, envir, enclos) 中的错误:objet ‘ratio’ introuvable 调用: … 处理 -> withCallingHandlers -> withVisible -> eval -> eval

R 没有找到“比率”对象! :( 你能帮我纠正这个for循环吗?

编辑:@user2554330 通过在循环之前添加 ratio

感谢大家的帮助!

【问题讨论】:

  • 错误是什么?请在帖子正文中逐字发布其消息。

标签: r for-loop kableextra


【解决方案1】:

在开始向其中添加元素之前,您需要创建ratio 列表。只需添加一行

ratio <- list()

在你的循环之前,事情应该没问题。 (当然,我无法测试这个建议,因为您没有发布可重现的示例......)

编辑添加: 现在该示例是可重现的(在稍微摆弄 dput 值以使其可用之后)。问题出在你的 for 循环中:

 for(i in JO_ratio_tranches[, grep("txcouv", names(JO_ratio_tranches))])

for 循环的标准格式是

 for(i in v)

v 是一个向量,但是你给了

 JO_ratio_tranches[, grep("txcouv", names(JO_ratio_tranches))]

这不是向量。我不确定您的意图是什么;也许你想要

 for(i in grep("txcouv", names(JO_ratio_tranches)))

改为?

【讨论】:

  • 非常感谢您的回答!有用 !不幸的是,出现了另一个问题! :( 我的 for 循环只显示最后一个循环的结果: ratio_8 .8_JO[22h_4h[_txcouv .8_JO[22h_4h[_txcouv] if(!is_empty(ratio_8)){voy_tranches[8,1]
  • 我怎样才能得到每个循环而不是最后一个循环的结果,好吗?再次感谢您的帮助!
  • 您应该编辑问题以做两件事:最重要的是,包含一些数据,以便它是一个可重复的示例。然后添加问题的第二部分。
  • 请使用dput 来生成我们可以运行的代码来制作您的数据集(或类似的数据集)。从屏幕截图输入数据太难了。
  • 您好 user2554330 !非常感谢您的回答!我还没有收到您更新消息的通知!这么晚才回复很抱歉 !我已经纠正了我的代码,但我想知道在这种情况下我是否不应该也做一个嵌套的 for 循环?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 1970-01-01
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多