我有以下数据框:
test2 <- data.frame(groups = c(rep("group1",4), rep("group2",4)),
X2 = c(rnorm(4), rnorm(4)) ,
label = c(rep(1,2),rep(2,2),rep(1,2),rep(2,2)))
Run Code Online (Sandbox Code Playgroud)
我正在使用以下方法绘制每组每个标签的条形图:
ggplot(test2, aes(label, X2, fill=as.factor(groups))) +
geom_bar(position="dodge", stat="identity")
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法找到一个stat="mean"
所以我可以在每个条形图而不是身份上绘制手段.
谢谢你的帮助.
我试图从字符串转换为uint64_t整数.stoi
返回一个32位整数,因此在我的情况下它不起作用.还有其他解决方案吗?
我试图source_gist
从devtools
包中使用但我遇到一个错误:
> library(devtools)
> source_gist("524eade46135f6348140")
Error in r_files[[which]] : invalid subscript type 'closure'
Run Code Online (Sandbox Code Playgroud)
谢谢你的建议.
我有一个矩阵:
mat <- matrix(c(0,0,0,0,1,1,1,1,-1,-1,-1,-1), ncol = 4 , nrow = 4)
Run Code Online (Sandbox Code Playgroud)
并且我应用以下函数来过滤掉只有正条目的列,但是对于具有负条目的列,我得到了一个NULL
.如何NULL
从输出中抑制s lapply
,apply
和sapply
?
> lapply(as.data.frame(mat), function(x) { if( all(x >= 0) ){return(x)} })
$V1
[1] 0 0 0 0
$V2
[1] 1 1 1 1
$V3
NULL
$V4
[1] 0 0 0 0
> sapply(as.data.frame(mat), function(x) { if( all(x >= 0) ){return(x)} })
$V1
[1] 0 0 0 0
$V2
[1] 1 1 1 1
$V3
NULL
$V4
[1] 0 0 …
Run Code Online (Sandbox Code Playgroud) 我有一个R包,我试图在MAC OS(约塞米蒂)上安装,我收到链接器问题.这是我得到的错误
clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o File1.so File2.o File3.o File4.o RcppExports.o Utils.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2 -lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: directory not found for option '-L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mypackage.so] Error 1
ERROR: compilation failed for package ‘mypackage’
Run Code Online (Sandbox Code Playgroud)
我的Makevars
文件包含:
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
Run Code Online (Sandbox Code Playgroud)
在 …
我正在尝试使用 OpenMP 并行化 for 循环,该循环对犰狳矩阵求和。我有以下代码:
#include <armadillo>
#include <omp.h>
int main()
{
arma::mat A = arma::randu<arma::mat>(1000,700);
arma::mat X = arma::zeros(700,700);
arma::rowvec point = A.row(0);
# pragma omp parallel for shared(A) reduction(+:X)
for(unsigned int i = 0; i < A.n_rows; i++){
arma::rowvec diff = point - A.row(i);
X += diff.t() * diff; // Adding the matrices to X here
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
[Legendre@localhost ~]$ g++ test2.cpp -o test2 -O2 -larmadillo -fopenmp
test2.cpp: In function ‘int main()’:
test2.cpp:11:52: error: user defined reduction …
Run Code Online (Sandbox Code Playgroud) 我正在使用R shiny-server
开源版本。我想计算应用程序的用户数。这是我的配置文件:
# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;
# Define a server that listens on port 80
server {
listen 80;
server_name some_name;
# Define a location at the base URL
location / {
# Host the directory of Shiny Apps stored in this directory
site_dir /srv/shiny-server/;
# Log all Shiny output to files in this directory
log_dir /var/log/shiny-server;
# When a user visits the base URL rather than a particular application, …
Run Code Online (Sandbox Code Playgroud) 在双for循环中使用迭代器的最佳方法是什么.对于单循环,显而易见的方式似乎是:
arma::vec test = arma::ones(10);
for(arma::vec::iterator i = test.begin(); i != test.end(); ++i){
int one = *i;
}
Run Code Online (Sandbox Code Playgroud)
所以我想改变以下内容:
arma::mat test = arma::ones(10,10);
for (int i = 0; i < test.n_rows; i++){
for (int j = 0; j < test.n_cols; j++){
int one = test(i,j);
}
}
Run Code Online (Sandbox Code Playgroud)
使用迭代器而不是整数索引.谢谢你的建议.
我试图找到一种更快的方法来将对添加到地图的末尾.目前,我在地图的末尾添加了对,因为我使用的键是for循环的索引,默认排序.所以我有:
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
int main()
{
std::map<int, std::set<int> > temp;
for(int i = 0; i < 1000000; i++){
int first[] = {5,10,15,20,25};
int second[] = {10,20,30,40,50};
std::set<int> temp2;
temp2.insert(first, first + 5);
std::set<int> temp3;
temp3.insert(second, second + 5);
std::set<int> temp4;
std::set_union(temp2.begin(), temp2.end(), temp3.begin(), temp3.end(), std::inserter(temp4, temp4.end()));
temp.insert(temp.end(), std::pair<int, std::set<int> >(i, temp4));
}
}
Run Code Online (Sandbox Code Playgroud)
当我计时时,大约需要10秒钟.但是,当我注释掉该行时temp.insert(temp.end(), std::pair<int, std::set<int> >(i, temp4))
,程序执行大约需要4秒钟.我想知道为什么将这对添加到地图需要花费很多时间.我是以最好的方式做到的吗?
我试图以与此程序类似的方式有条件地初始化引用变量,但不可能以下列方式执行:
#include <vector>
void test(bool val){
std::vector<std::vector<int> > mat(10, std::vector<int>(10, 0));
std::vector<std::vector<int> > &ref_mat; // This is not allowed
if(val){
ref_mat = mat;
}else{
std::vector<std::vector<int> > mat2 = Somefunction(); // The returned 2D vector from some function
ref_mat = mat2;
}
}
Run Code Online (Sandbox Code Playgroud)
可以用不同的方式完成吗?我需要只mat2
在必要时创建一个新对象,这样我就可以节省内存.
我在 python 中实现了 16 位整数的补码加法,但是我想看看是否有更好的方法来做到这一点。
# This function returns a string of the bits (exactly 16 bits)
# for the number (in base 10 passed to it)
def get_bits(some_num):
binar = bin(some_num)[2::]
zeroes = 16 - len(binar)
padding = zeroes*"0"
binar = padding + binar
return binar
# This function adds the numbers, and handles the carry over
# from the most significant bit
def add_bits(num1, num2):
result = bin(int(num1,2) + int(num2,2))[2::]
# There is no carryover
if len(result) <= …
Run Code Online (Sandbox Code Playgroud)