- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从 ifelse()
得到了一个完全出乎意料的结果,如果能解释原因,我将不胜感激。请参阅底部的可重现数据。
split_ratio = 0.8
target_label = "DV"
training.index <- caret::createDataPartition(dataset[[target_label]], p = split_ratio, list = FALSE)
training.set <- dataset[training.index, ]
以下按预期工作:
testing.set <- if (split_ratio != 1.0) dataset[-training.index, ] else NULL
testing.set
# A tibble: 2 x 6
DV nn ee oo aa cc
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 89 87 135 112 118 139
2 80 82 134 111 136 128
假设相同的代码,包裹在 ifelse 中,返回一个包含向量的列表
testing.set <- ifelse(split_ratio != 1.0, dataset[-training.index, ], NULL)
testing.set
[[1]]
[1] 89 80
是否在 ifelse 中不允许对数据帧/tibble 进行子集化,还是有其他原因?从帮助页面看并不是很明白...
这是上面变量 dataset
的可重现内容:
structure(list(DV = c(98, 89, 93, 80, 80, 65, 92, 85, 80, 90,
77, 80, 80, 75, 90, 88, 88, 90, 90, 88), nn = c(65, 87, 61, 82,
67, 75, 79, 56, 82, 80, 63, 77, 68, 82, 82, 87, 83, 73, 60, 60
), ee = c(149, 135, 149, 134, 153, 143, 129, 167, 168, 121, 138,
136, 129, 141, 116, 135, 142, 122, 134, 145), oo = c(118, 112,
109, 111, 79, 118, 101, 107, 134, 112, 125, 120, 108, 125, 110,
116, 94, 93, 108, 104), aa = c(129, 118, 140, 136, 99, 123, 119,
122, 122, 124, 89, 123, 120, 162, 116, 126, 140, 122, 129, 123
), cc = c(162, 139, 155, 128, 137, 126, 120, 154, 155, 143, 137,
137, 136, 138, 99, 119, 135, 138, 145, 147)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -20L), na.action = structure(c(`3` = 3L,
`11` = 11L, `15` = 15L, `17` = 17L, `19` = 19L, `20` = 20L, `29` = 29L,
`40` = 40L, `48` = 48L, `52` = 52L, `70` = 70L, `77` = 77L, `88` = 88L,
`119` = 119L, `124` = 124L, `152` = 152L, `163` = 163L, `169` = 169L,
`182` = 182L, `192` = 192L, `219` = 219L, `225` = 225L, `242` = 242L,
`244` = 244L, `247` = 247L, `253` = 253L, `265` = 265L, `267` = 267L,
`274` = 274L, `309` = 309L, `317` = 317L, `324` = 324L, `330` = 330L,
`341` = 341L, `364` = 364L, `366` = 366L, `386` = 386L, `411` = 411L,
`421` = 421L, `426` = 426L, `430` = 430L, `437` = 437L, `440` = 440L,
`450` = 450L, `454` = 454L, `460` = 460L, `462` = 462L, `476` = 476L,
`483` = 483L, `505` = 505L, `506` = 506L, `515` = 515L, `533` = 533L,
`535` = 535L, `540` = 540L, `552` = 552L, `563` = 563L, `578` = 578L,
`584` = 584L, `589` = 589L, `594` = 594L, `596` = 596L, `597` = 597L,
`604` = 604L, `609` = 609L, `614` = 614L, `671` = 671L, `683` = 683L,
`688` = 688L, `701` = 701L, `702` = 702L, `713` = 713L, `715` = 715L,
`719` = 719L, `752` = 752L, `773` = 773L, `793` = 793L, `794` = 794L,
`795` = 795L, `799` = 799L, `800` = 800L, `817` = 817L, `823` = 823L,
`829` = 829L, `834` = 834L, `849` = 849L, `850` = 850L, `851` = 851L,
`854` = 854L, `875` = 875L, `882` = 882L, `891` = 891L, `892` = 892L,
`895` = 895L, `910` = 910L, `924` = 924L, `925` = 925L, `936` = 936L,
`955` = 955L, `958` = 958L, `968` = 968L, `972` = 972L, `984` = 984L,
`989` = 989L, `991` = 991L, `992` = 992L, `994` = 994L, `1007` = 1007L,
`1018` = 1018L, `1029` = 1029L, `1030` = 1030L, `1049` = 1049L,
`1065` = 1065L, `1084` = 1084L, `1085` = 1085L, `1086` = 1086L,
`1095` = 1095L, `1096` = 1096L, `1097` = 1097L, `1100` = 1100L,
`1102` = 1102L, `1110` = 1110L, `1117` = 1117L, `1125` = 1125L,
`1127` = 1127L, `1145` = 1145L, `1160` = 1160L, `1161` = 1161L,
`1164` = 1164L, `1166` = 1166L, `1171` = 1171L, `1187` = 1187L,
`1191` = 1191L, `1194` = 1194L, `1212` = 1212L, `1215` = 1215L,
`1239` = 1239L, `1254` = 1254L, `1262` = 1262L, `1263` = 1263L,
`1274` = 1274L, `1297` = 1297L, `1308` = 1308L, `1325` = 1325L,
`1328` = 1328L, `1331` = 1331L, `1337` = 1337L, `1338` = 1338L,
`1340` = 1340L, `1342` = 1342L, `1348` = 1348L, `1354` = 1354L,
`1361` = 1361L, `1367` = 1367L, `1373` = 1373L, `1379` = 1379L,
`1389` = 1389L, `1406` = 1406L, `1411` = 1411L, `1422` = 1422L,
`1423` = 1423L, `1436` = 1436L, `1439` = 1439L, `1441` = 1441L,
`1446` = 1446L, `1449` = 1449L, `1476` = 1476L, `1480` = 1480L,
`1481` = 1481L, `1483` = 1483L, `1503` = 1503L, `1511` = 1511L,
`1516` = 1516L, `1521` = 1521L, `1524` = 1524L, `1527` = 1527L,
`1544` = 1544L, `1550` = 1550L, `1567` = 1567L, `1580` = 1580L,
`1582` = 1582L, `1586` = 1586L, `1595` = 1595L, `1601` = 1601L,
`1609` = 1609L, `1612` = 1612L, `1619` = 1619L), class = "omit"))
最佳答案
ifelse
是一个向量化函数。参见 ?ifelse
:
ifelse returns a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is TRUE or FALSE.
由于split_ratio != 1.0
是一个向量,其第一个值为TRUE
,因此返回值的第一个值取自dataset[-training.index , ]
,即dataset[-training.index, ][1]
。因此,您得到了一个长度为 1 的列表。
ifelse 和 if{}else{}
不相等。该文档明确为您推荐 if{} else{}
案例:
Further note that if(test) yes else no is much more efficient and often much preferable to ifelse(test, yes, no) whenever test is a simple true/false result, i.e., when length(test) == 1.
关于r - ifelse 不对数据帧执行子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57901273/
我有一个默认的表 white-space : normal想申请white-space: nowrap对于链接到特定 th 的所有 td 元素,但不必将其应用于 元素本身,而不是使用外部 css 文件
我在一篇有关 Version Insight ( http://www.delphifeeds.com/go/s/77066 ) 的博客中读到(除其他外)JCL 没有受版本控制的 .dproj 文件,
我正在打开一个弹出窗口,并希望在其中执行单击操作,从而在打开它的页面中执行一些 jQuery。我在网上找到的所有内容都表明我应该能够使用 window.opener 做到这一点(例如 JQuery -
tablesorter 不适用于主题列,当列包含“-”时,例如: Name Subject Anton - Max "dfdsrv" Anna "fdsf" 但如果我添加带有规范符
我从 web 服务中获取了如下顺序的数据 234,00234,000,00 但是…… 如果 xml 更改为 NSMutableDictionary,它会转到排序顺序。 "Resultat_detail
我想在不执行循环的情况下从 pandas 列的值中提取第一个 3。 所以, df['myCol'][3] 5475 为了提取前 3 位数字,我这样做: int(str(df['myCol'][3])[
我一直读到python有严格的类型检查- >>> 1 + 'hello' Traceback (most recent call last): File "", line 1, in TypeE
在 MySQL 查询中,例如 SELECT * FROM ( SELECT user_id FROM favorites WHERE user_id >1 UNION SELECT user_id F
我有一个 UIScrollView,里面有一些标签。我可以使用按钮将 ScrollView 移动到另一个“页面”。但是当我推得太快时,偏移量不正确。 我将 ScrollView 移动到下一页的代码:
我正在尝试设置 SonarQube在成功构建 Travis 后评论我的 GitHub 拉取请求。 我已经有正常的分析工作。对于拉取请求分析,我还准备了所有 token ,安装了插件等。拉取请求中的问题
这里有一些代码。同样的模式(afaik)适用于英雄教程。 login.component.html: Invalid credentials login.component
我很困惑。我见过一些类似的问题,但没有一个能解决我的问题;所以我在网上抓取了这个脚本,它通过运行 makefile 自动压缩 javascript 文件,如下所示: concatenated.min.
我们在 url 参数中需要一个编码的分号字符,但 angular 不编码这个字符。 我们使用的资源如下所示: app.factory('TestResource', ['$resource', fun
我对 Hibernate 有一个奇怪的问题。我可以从数据库中选择一些东西,但我不能插入或更新任何值。这是我的配置和示例代码,persistance.xml: ****Us
类似于这个问题:group by not-null values我试图只对列 groupID 不为空的记录进行分组: +--+-------+------+-----+-----+----------
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: how to not apply opacity for child element? 哪个是设置不透明度的
我在我正在构建的 C++ 应用程序中使用 libtorrent,下载工作完美,但我想在尝试开始下载之前询问跟踪器它已连接的种子和对等点的数量。 我尝试使用 scrape_tracker(),但我从未收
我……很困惑。事情是这样的。我有一个编码为 UNICODE (Little Endian) 的 *ini 文件。在我的 Visual Studio 项目(我自己的 ini 解析器)中,我正在检查文本文
当我将 Linq-to-sql 查询绑定(bind)到 datagridview(在两者之间使用 BindingSource)时,列默认是可排序的。但是, bool 类型似乎并非如此。对于这些数据 G
当我将 iPhone 图像上传到我的 Wordpress 网站时,用 iPhone 拍摄的图像旋转错误。在我的电脑上旋转是正确的,但上传时旋转出错了。 有什么想法吗? 更新:Wordpress UI
我是一名优秀的程序员,十分优秀!