当存在多个分组变量时,group_by 的数据屏蔽不起作用。
下面粘贴代码
grpByCols <- "model"
mpg%>%
group_by(.data[[grpByCols]])
grpByCols <- c("model", "manufacturer")
mpg%>%
group_by(.data[[grpByCols]])
Run Code Online (Sandbox Code Playgroud)
第一个 group_by 有效,第二个失败。
粘贴下面的运行输出
> grpByCols <- "model"
>
> mpg%>%
+ group_by(.data[[grpByCols]])
# A tibble: 234 x 11
# Groups: model [38]
manufacturer model displ year cyl trans drv cty hwy fl class
<chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
2 audi a4 1.8 1999 4 manual(m5) f 21 …Run Code Online (Sandbox Code Playgroud) 我需要根据列值计算与特定行不同的行数,同时避免循环解决方案。
示例:取一个有四行的data.table对象,其中每行代表一个人。A每个人都有3个价值观v1, v2, v3。目标是计算对于特定个体(行),有多少其他个体的所有三个变量的值都严格大于(或等于),并计入变量 中count。完成这项工作的循环版本可能是:
A = data.table(matrix(0, nrow = 4, ncol = 3))
colnames(A) <- c("v1","v2","v3")
# Assign values for variables v1, v2, v3
A[1,1] <- 1; A[1,2] <- 1; A[1,3] <- 1
A[2,1] <- 1; A[2,2] <- 1.5; A[2,3] <- 1
A[3,1] <- 0.9; A[3,2] <- 0.5; A[3,3] <- 0.8
A[4,1] <- 2; A[4,2] <- 1.5; A[4,3] <- 2
# Count variable
A$count = NA
for(j in 1:nrow(A)){
A$count[j] = …Run Code Online (Sandbox Code Playgroud) 我正在尝试用几何图形在 ggplot 中制作图表geom_sf。我可以使用 来绘制图表scale_fill_scontinuous,但是我想将标题分成离散的间隔,这样scale_fill_scontinuous我就可以使用 来代替使用scale_fill_brewer。\n我不想在 ggplot 之外创建中间 data.frame 或向量。
目前我的图表如下所示:
\nggplot(data=uniao3, aes(fill=`\xc3\x81rea \xc3\x9amida`))+\n geom_sf()+\n scale_fill_continuous(limits=c(0,20000),\n breaks=c(500,2000,8000,15000,20000),\n labels=c(\'500\',\'200-2k\',\'2k-8k\',\'8k-15k\',\'15k>\'),\n name="Wetland")+\n facet_wrap(~Ano)\nRun Code Online (Sandbox Code Playgroud)\n\n我希望将图例(比例)呈现为渐变,而不是呈现为类似于此的块:
\n\n我知道该scale_fill_brewer命令非常优雅且高效地执行此操作,但是如何将连续变量设置为 ggplot 内的离散变量?
我有以下结构:
template<typename T>
struct Foo
{
typename T::iterator iter;
};
Run Code Online (Sandbox Code Playgroud)
预期类型:
iter是 astd::map<K, V>::iterator时T推导为std::map<K, V>。
iter是一个std::map<K, V>::const_iterator何时T推导为const std::map<K, V>
但我的代码总是得到一个std::map<K, V>::iterator.
如何达到预期的实施效果?
我正在尝试将当前数据集的格式更改为每行 1 个用户的格式,并将“颜色”和“食物”列中的所有唯一值(动态数量的值)拆分为各自包含“是”和“否”的列。用户有一个唯一的ID。
Current format:
ID | Name | Color | Food
1 | John | Blue | Pizza
1 | John | Red | Pizza
1 | John | Yellow | Pizza
1 | John | Blue | Ice Cream
1 | John | Red | Ice Cream
1 | John | Yellow | Ice Cream
2 | Kelly | Blue | Pizza
2 | Kelly | Red | Pizza
Desired format:
ID | Name | Color_Blue | Color_Red …Run Code Online (Sandbox Code Playgroud) #include <utility>
class A {
int* ptr;
public:
A() {
ptr = new int[10];
}
A(A&& rhs) {
this->ptr = rhs.ptr;
rhs.ptr = nullptr;
}
~A() {
if (ptr) delete[] ptr;
}
};
int main() {
A a1;
A a2;
a2 = std::move(a1);
}
Run Code Online (Sandbox Code Playgroud)
GCC 11 抱怨复制赋值运算符被删除:
<source>: In function 'int main()':
<source>:23:22: error: use of deleted function 'constexpr A& A::operator=(const A&)'
23 | a2 = std::move(a1);
| ^
<source>:3:7: note: 'constexpr A& A::operator=(const A&)' is implicitly declared as deleted …Run Code Online (Sandbox Code Playgroud) 我正在学习计算机体系结构,并决定尝试乘法溢出。观察到 的溢出INT_MAX * INT_MAX,但我不确定为什么这会在 C/C++ 中给出乘积 1。
#include <stdio.h>
#include <limits.h>
int main()
{
int num = INT_MAX;
printf("%0x\n", num); //stdout is 0x7fffffff
printf("%d\n", num * num); //stdout is 1
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在下面的示例中,struct A没有默认构造函数。因此,从它继承的struct B和 都struct C无法获得编译器生成的默认构造函数:
struct A {
A(int) {}
};
struct B : A {
B() = default; //#1
};
struct C : A {
C();
};
C::C() = default; //#2
Run Code Online (Sandbox Code Playgroud)
#1. 在 中struct B,默认的默认构造函数在类内部声明,并且所有编译器都接受它,只有 Clang 显示警告说:explicitly defaulted default constructor is implicitly deleted [-Wdefaulted-function-deleted]
#2. 但是在外部声明的默认构造函数struct C会生成编译器错误。演示: https: //gcc.godbolt.org/z/3EGc4rTqE
为什么默认构造函数的位置很重要:在类内部还是外部?
当编写 CSS 时,如果您有多个<P>标签,所有标签都需要单独的颜色,您是否只是继续编写单独的<P>选择器,还是需要有一个元素/选择器来分隔它们?
如果第三列中的值为负数,我想交换/切换两列中的值。
id <- c("1", "2", "3", "4", "5")
effect <- c("A", "C", "G", "A", "G")
non_effect <- c("C", "A", "T", "C", "C")
variable1 <- c("X", "X", "X", "X", "X")
variable2 <- c("Y", "Y", "Y", "Y", "Y")
value <-c("0.2", "-0.1", "-0.3", "0.5", "0.8")
df <- data.frame(id,effect,non_effect,variable1,variable2,value)
Run Code Online (Sandbox Code Playgroud)
我会将其作为 data.frame 获取
id effect non_effect variable1 variable2 value
1 A C X Y 0.2
2 C A X Y -0.1
3 G T X Y -0.3
4 A C X Y 0.5
5 G …Run Code Online (Sandbox Code Playgroud)