我有一个这样的数据框:
df <- data.frame(var1 = c(1,1,3,4,5,6,7,8,9),
var2 = c(11,11,33,44,55,66,77,88,99),
var3 = c(111,111,333,444,555,666,777,888,999),
var4 = c(1111,1111,3333,4444,5555,6666,7777,8888,9999))
> df
var1 var2 var3 var4
1 1 11 111 1111
2 1 11 111 1111
3 3 33 333 3333
4 4 44 444 4444
5 5 55 555 5555
6 6 66 666 6666
7 7 77 777 7777
8 8 88 888 8888
9 9 99 999 9999
Run Code Online (Sandbox Code Playgroud)
我想根据存储在列表中的多个列值过滤特定行。
例如:
my_list <- list(var1 = 1,
var2 = 11,
var3 = 111)
filtered_df …Run Code Online (Sandbox Code Playgroud) 我已经为伽马和对数正态分布的矩设置了一些初始参数,并应用Kolmogorov-Smirnov检验来获得p值.我的目的是显示针对不同N的p值与N的关系图.可以说在5到1000之间.我将如何实现这一目标?
mean <- 10
var <- 40
N <- 100
gamsample <- rgamma(N, shape=mean^2/var, rate=mean/var)
lnsample <- rlnorm(N, meanlog=log(mean)-log(1+mean^2/var)/2,
sdlog=sqrt(log(1+(mean^2/var))))
ks.test(gamsample, lnsample)$p.value
Run Code Online (Sandbox Code Playgroud) 在summarise_at()中使用n()时,出现以下错误:
Error: n() should only be called in a data context
Call `rlang::last_error()` to see a backtrace
Run Code Online (Sandbox Code Playgroud)
其他人认为这可能是dplyrwith 的掩盖问题plyr,两个解决方案是:
summarise_at()为dplyr :: summarise_at()detach("package:plyr", unload=TRUE)都没有消除此错误,我很想知道是什么原因引起的。这是一个可重现的示例,应导致相同的错误:
Df <- data.frame(
Condition = c(rep("No", 20), rep("Yes",20)),
Height = c(rep(1,10),rep(2,10),rep(1,10),rep(2,10)),
Weight = c(rep(10,5),rep(20,5),rep(30,5), rep(40,5))
)
x <- c("Height","Weight")
Df %>%
group_by(Condition) %>%
summarise_at(vars(one_of(x)), c(mean = mean, sd = sd, count = n()))
Run Code Online (Sandbox Code Playgroud)
注意:如果删除count = n()该代码,运行不会出现任何问题