我正在尝试解决黑客排名上的“一笔非常大的金额”挑战:https://www.hackerrank.com/challenges/a-very-big-sum/problem
\n在那里我必须对给出的数组中的所有数字求和,所以我想出了两个解决方案:
\n第一个解决方案
\nfunction aVeryBigSum(ar){\n let sum = 0;\n for(let i = 0; i < ar.length; i++){\n sum += i;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n第二种解决方案
\nfunction(ar){\n let sum = ar.reduce((accumulator, currentValue) => {\n accumulator + currentValue;\n });\n}\nRun Code Online (Sandbox Code Playgroud)\n但它们都不起作用,我不\xc2\xb4t知道为什么,我想也许我没有按照黑客等级希望我写的那样,但我不确定
\n如何将一个整数随机划分为 n 个部分,且可能的结果为零?(最好是R语言)
例如,要将整数 5 分成 3 部分并执行 4 次,我可能会得到以下输出:
[1] 4 0 1
[2] 2 2 1
[3] 0 2 3
[4] 1 1 3
Run Code Online (Sandbox Code Playgroud)
谢谢!
我尝试使用 F1 分数来确定哪个 k 值可以最大化模型以达到其给定的目的。模型是通过包train中的函数制作的caret。
示例数据集: https: //www.kaggle.com/lachster/churndata
我当前的代码包括以下内容(作为 f1 分数的函数):
f1 <- function(data, lev = NULL, model = NULL) {
precision <- posPredValue(data$pred, data$obs, positive = "pass")
recall <- sensitivity(data$pred, data$obs, positive = "pass")
f1_val <- (2*precision*recall) / (precision + recall)
names(f1_val) <- c("F1")
f1_val
}
Run Code Online (Sandbox Code Playgroud)
以下为列车控制:
train.control <- trainControl(method = "repeatedcv", number = 10, repeats = 3,
summaryFunction = f1, search = "grid")
Run Code Online (Sandbox Code Playgroud)
以下是我最终执行的命令train:
x <- train(CHURN ~. ,
data = experiment, …Run Code Online (Sandbox Code Playgroud) 我有这个数据:
merged_dat1
# A tibble: 4 x 35
# Groups: Product.Name [4]
Product.Name also apps battery better big camera case cheap day definitely enough even however
<chr> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 BLU Studio ~ 498 545 1552 465 306 839 406 161 413 225 156 590 178
2 iphone 4s 71 37 380 43 19 13 21 38 43 25 16 128 52
3 Motorola Mo~ 160 221 253 123 …Run Code Online (Sandbox Code Playgroud) 由于 的构造函数std::shared_ptr被标记为显式构造函数,因此 like 的表达式auto p = std::make_shared<int>(1); p = new int(6);是错误的。
我的问题是为什么std::make_shared<int>(1); p = nullptr;编译?
这是前面提到的代码片段:
#include <memory>
#include <iostream>
int main()
{
auto p = std::make_shared<int>(1);
//p = new int(6);
p = nullptr;
if(!p)
{
std::cout << "not accessable any more" << std::endl;
}
p.reset();
}
Run Code Online (Sandbox Code Playgroud)
我想要一个对象,它为我提供从 1990-01-01 到 2021-12-31 的每个月(或季度)的日期范围,并用冒号分隔。例如,在每月的情况下,第一个对象将是 1990-01-01:1990-01-31,第二个对象将是 1990-02-01:1990-02-31,依此类推。
我遇到的问题是确保日期范围是排他的,即没有重复的日期。
start_date1 <- as.Date("1990-01-01", "%Y-%m-%d")
end_date1 <- as.Date("2021-12-01", "%Y-%m-%d")
first_date <- format(seq(start_date1,end_date1,by="month"),"%Y-%m-%d")
start_date2 <- as.Date("1990-02-01", "%Y-%m-%d")
end_date2 <- as.Date("2022-01-01", "%Y-%m-%d")
second_date <- format(seq(start_date2,end_date2,by="month"),"%Y-%m-%d")
date<-paste0(first_date, ":")
finaldate<-paste0(date, second_date)
Run Code Online (Sandbox Code Playgroud)
此代码有效,除了每个月的第一个日期重复“1990-01-01:1990-02-01”“1990-02-01:1990-03-01”,最后一个日期是“2021- 12-01:2022-01-01”(包括 2022 年 1 月 1 日,而不是停止于 2021 年 12 月 31 日。
如果我改为 30 天,效果就不太好,因为不是每个月都有 30 天。
获得独家日期范围的最佳方式是什么?
我想根据匹配检查列表中的相邻元素。例如,在随机排序的字母列表中,我想知道 的相邻字母是什么m。我当前的解决方案是:
library(stringr)
ltrs <- sample(letters)
ltrs[(str_which(ltrs,'m')-2):(str_which(ltrs,'m')+2)]
[1] "j" "f" "m" "q" "a"
Run Code Online (Sandbox Code Playgroud)
对我来说,重复str_which()感觉没有必要。有没有更简单的方法来达到相同的结果?
安装 R 版本 4.2.0 后,我突然无法从 CRAN 安装软件包。这仅在我的具有强大防火墙的办公室站点上是正确的。
\n警告消息粘贴如下:
\n> Warning in install.packages: unable to access index for repository\n> http://cran.rstudio.com/src/contrib: cannot open URL\n> 'http://cran.rstudio.com/src/contrib/PACKAGES' Installing package into\n> \xe2\x80\x98C:/Users/l2863/AppData/Local/R/win-library/4.2\xe2\x80\x99 (as \xe2\x80\x98lib\xe2\x80\x99 is\n> unspecified) Warning in install.packages: unable to access index for\n> repository http://cran.rstudio.com/src/contrib: cannot open URL\n> 'http://cran.rstudio.com/src/contrib/PACKAGES'\nRun Code Online (Sandbox Code Playgroud)\n我查看了类似的问题,但没有找到任何有效的解决方案。
\n我的 df 具有以下 colname:
colname(df) 给出:
"SUBJID" "EoT_A" "EoT_B" "EoT_C" "EoT_D" "PR_A" "PR_B" "PR_C" "PR_D"
"PD_A" "PD_B" "PD_C" "PD_D" "CR_A" "CR_B" "CR_C" "CR_D"
Run Code Online (Sandbox Code Playgroud)
我想重新排序 colname,例如:
"SUBJID"
"EoT_A" "PR_A" "PD_A" "CR_A"
"EoT_B" "PR_B" "PD_B" "CR_B"
"EoT_C" "PR_C" "PD_C" "CR_C"
"EoT_D" "PR_D" "PD_D" "CR_D"
Run Code Online (Sandbox Code Playgroud)
有没有一种聪明的方法可以实现这一目标?
让我们考虑这个简单的数据集
set.seed(12345)
df <- data.frame(a1 = rnorm(5), a2 = rnorm(5), a3 = rnorm(5),
b1 = rnorm(5), b2 = rnorm(5), b3 = rnorm(5),
c1 = rnorm(5), c2 = rnorm(5), c3 = rnorm(5))
Run Code Online (Sandbox Code Playgroud)
看起来像
a1 a2 a3 b1 b2 b3 c1 c2 c3
1 0.5855288 -1.8179560 -0.1162478 0.8168998 0.7796219 1.8050975 0.8118732 0.49118828 1.1285108
2 0.7094660 0.6300986 1.8173120 -0.8863575 1.4557851 -0.4816474 2.1968335 -0.32408658 -2.3803581
3 -0.1093033 -0.2761841 0.3706279 -0.3315776 -0.6443284 0.6203798 2.0491903 -1.66205024 -1.0602656
4 -0.4534972 -0.2841597 0.5202165 1.1207127 -1.5531374 0.6121235 1.6324456 1.76773385 …Run Code Online (Sandbox Code Playgroud) r ×8
dplyr ×2
arrays ×1
c++ ×1
c++11 ×1
date ×1
date-range ×1
explicit ×1
grouping ×1
indexing ×1
javascript ×1
r-caret ×1
reduce ×1
shared-ptr ×1
stringr ×1
tidyselect ×1