我正在阅读有关统计学习/机器学习和 R 的这本书。问题之一是:
首先,加载波士顿数据集。波士顿数据集是 R 中 MASS 库的一部分。
library (MASS)
Run Code Online (Sandbox Code Playgroud)
现在数据集包含在对象中
Boston。阅读有关数据集的信息:
?Boston
我不明白语法library(MASS)。我如何从中获得波士顿数据集?我试过了,Boston=library(MASS)但这给了我一系列的词:
"MASS" "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"
Run Code Online (Sandbox Code Playgroud)
我也尝试过类似的东西,Boston=library(MASS::Boston)但这似乎也无效。
我正在尝试为3个手动添加的行添加相应的图例ggplot.我的代码如下:
library(ggplot2)
df = data.frame(error = c(0.0832544999, 0.0226680026, 0.0082536264, 0.0049199958, 0.0003917755, 0.0003859976, 0.0003888253, 0.0003953918, 0.0003958398), sDev = c(8.188111e-03, 2.976161e-03, 1.466221e-03, 2.141425e-03, 2.126976e-05, 2.139364e-05, 2.169059e-05, 2.629895e-05, 2.745938e-05))
minimum <- 6
best.model <- 5
gplot <- ggplot(df, aes(x=1:length(error), y=error)) +
scale_x_continuous(breaks = seq_along(df$error)) +
geom_point(size = 3) +
geom_line() +
geom_errorbar(data = df, aes(x = 1:length(error), ymin = error - sDev, ymax = error + sDev),
width = 0.1) +
geom_hline(data = df, aes(yintercept = error[minimum] + sDev[minimum]), linetype = …Run Code Online (Sandbox Code Playgroud) 我现在正在学习R,我遇到了查找命令的问题.
我有分类数据
levels(job)
[1] "admin." "blue-collar" "entrepreneur" "housemaid"
[5] "management" "retired" "self-employed" "services"
[9] "student" "technician" "unemployed" "unknown"
Run Code Online (Sandbox Code Playgroud)
现在我想简化这些级别,例如
levels(job)
[1] "class1" "class2" "class3" "unknown"
Run Code Online (Sandbox Code Playgroud)
其中type1包括"admin.","entrepreneur",和"self-employed";
type2包括"blue-collar","management",和"technician";
type3包括"housemaid","student","retired",和"services";
unknown包括"unknown"和"unemployed".
为此,我可以使用哪个命令?谢谢!严
我正在努力将数据从二进制文件导入到 R 中。其中,数据文件包含一个我需要读出的64 位无符号整数值(小端) 。
该值表示自 1900 年 1 月 1 日凌晨 12:00 起的秒数,并将转换为日期和时间值(例如 YYMMDDhhmmss)。
作为初学者,我读过一些软件包(bit64、int64),但它们似乎不支持无符号整数或不再维护。
有人可以帮帮我吗?
我想用ggplot2绘制一个xts对象,但是收到错误.这是我正在做的事情:
dates <- c("2014-10-01", "2014-11-01", "2014-12-01", "2015-01-01", "2015-02-01")
value <- as.numeric(c(3, 4, 5, 6, 5))
new_df <- data_frame(dates, value)
new_df$dates <- as.Date(dates)
new_df <- as.xts(new_df[,-1], order.by = new_df$dates)
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用ggplot2绘制它:
ggplot(new_df, aes(x = index, y = value)) + geom_point()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误(函数(...,row.names = NULL,check.rows = FALSE,check.names = TRUE,:参数意味着不同的行数:0,5
我不太确定我做错了什么.
开始使用magrittr管道运算符,如果可以在单个流程中创建两个数据框,那就很奇怪了.例如,生成用于绘图的非聚合数据框和用于订购因子的聚合数据框(聚合排序示例)将是有帮助的.
这是一个相当人为的例子,说明了这个问题:
library(dplyr)
library(tidyr)
library(magrittr)
library(ggplot2) # msleep
vore_count <-
na.exclude(msleep) %>%
group_by(vore, order) %>%
summarise(count = n()) %>%
ungroup()
agg <- vore_count %>%
spread(vore, count)
Run Code Online (Sandbox Code Playgroud)
既可以vore_count 与 agg在同一个流程中产生的?
我尝试了以下(以及使用%T>%),这显然不起作用.
vore_count <-
na.exclude(msleep) %>%
group_by(vore, order) %>%
summarise(count = n()) %>%
ungroup() %>%
agg <- spread(vore, count)
Run Code Online (Sandbox Code Playgroud) 关于这个主题有很多讨论.我经历过他们,但没有人帮忙.
问题似乎很简单:
如果我们列出10以下的所有自然数是3或5的倍数,我们得到3,5,6和9.这些倍数的总和是23.
找出N以下3或5的所有倍数之和.
输入格式第一行包含表示测试用例数的T. 接下来是T行,每行包含一个整数N.
输出格式对于每个测试用例,打印一个整数,表示N以下所有3或5的倍数之和.
约束1≤T≤10^ 51≤N≤10^ 9
但是,对于两个测试用例,最有可能是输入较大的测试用例,我的代码会因超时而终止.
这是我的代码:
int main() {
unsigned long long int n,t;
unsigned long long int sum;
cin>>t;
while(t--)
{
sum=0;
cin>>n;
for(unsigned long long int i=3;i<n;i++){
if(i%3==0 || i%5==0){
sum+=i;
}
}
cout<<sum<<"\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
即使使用unsigned long long int,为什么它不适用于大输入?
我正在为特定问题开发自定义引导算法,因为我想要大量的重复,我确实关心性能.在这方面,我对如何正确使用runif有一些疑问.我知道我自己可以运行基准测试,但C++优化往往很难,我也想了解任何差异的原因.
第一个问题:
第一个代码块是否比第二个更快?
for (int i = 0; i < n_boot; i++) {
new_random = runif(n); //new_random is pre-allocated in class
// do something with the random numbers
}
Run Code Online (Sandbox Code Playgroud)
for (int i = 0; i < n_boot; i++) {
NumericVector new_random = runif(n);
// do something with the random numbers
}
Run Code Online (Sandbox Code Playgroud)
它可能归结为runif是填充左侧还是分配并传递新的NumericVector.
第二个问题:
如果两个版本都分配了一个新的向量,我可以通过在标量模式下一次生成一个随机数来改进吗?
如果您想知道,内存分配占用了我处理时间的相当大一部分.通过优化其他不必要的内存分配,我将运行时间减少了30%,因此它很重要.
我有一个很长的参数向量(大约4 ^ 10个元素)和一个索引向量.我的目标是将索引向量中索引的所有参数值加在一起.
例如,如果我有para = [1,2,3,4,5,5,5]和indices = [3,3,1,6]那么我想找到第三个值的累积和(3 )两次,第一个值(1)和第六个(5),得到12.另外还有根据它们的位置扭曲参数值的选项.
我正在尝试加速R实现,因为我称之为数百万次.
我当前的代码总是返回NA,我无法看到它出错的地方
这是Rcpp函数:
double dot_prod_c(NumericVector indices, NumericVector paras,
NumericVector warp = NA_REAL) {
int len = indices.size();
LogicalVector indices_ok;
for (int i = 0; i < len; i++){
indices_ok.push_back(R_IsNA(indices[i]));
}
if(is_true(any(indices_ok))){
return NA_REAL;
}
double counter = 0;
if(NumericVector::is_na(warp[1])){
for (int i = 0; i < len; i++){
counter += paras[indices[i]];
}
} else {
for (int i = 0; i < len; i++){
counter += paras[indices[i]] * warp[i]; …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚如何用Rcpp连接2个字符串; 当我怀疑有一个明显的答案时,文档没有帮助我.
http://gallery.rcpp.org/articles/working-with-Rcpp-StringVector/
http://gallery.rcpp.org/articles/strings_with_rcpp/
StringVector concatenate(StringVector a, StringVector b)
{
StringVector c;
c= ??;
return c;
}
Run Code Online (Sandbox Code Playgroud)
我希望这个输出:
a=c("a","b"); b=c("c","d");
concatenate(a,b)
[1] "ac" "bd"
Run Code Online (Sandbox Code Playgroud)