小编Mar*_*Gal的帖子

rnaturalearthhires 包安装错误

我正在尝试安装该rnaturalearthhires软件包,但遇到很多错误。

\n

这是第一个错误:

\n
Installing the rnaturalearthhires package.\nInstalling package into \xe2\x80\x98C:/Users/kkost/OneDrive/Dokumenti/R/win-library/4.0\xe2\x80\x99\n(as \xe2\x80\x98lib\xe2\x80\x99 is unspecified)\n
Run Code Online (Sandbox Code Playgroud)\n
\n

值 [3L] 中存在错误:\n无法安装 rnaturalearthhires 软件包。\n请尝试使用以下命令自行安装该软件包:\ninstall.packages("rnaturalearthhires", repos = http://packages.ropensci.org", type = "source")

\n
\n

然后我尝试以下代码:

\n
install.packages("rnaturalearthhires", repos = "http://packages.ropensci.org", type = "source")\n
Run Code Online (Sandbox Code Playgroud)\n

我明白了:

\n
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:\nhttps://cran.rstudio.com/bin/windows/Rtools/\nInstalling package into \xe2\x80\x98C:/Users/kkost/OneDrive/Dokumenti/R/win-library/4.0\xe2\x80\x99\n(as \xe2\x80\x98lib\xe2\x80\x99 is unspecified)\n**Error in install.packages : error reading from …
Run Code Online (Sandbox Code Playgroud)

r

9
推荐指数
2
解决办法
6173
查看次数

为什么 mapply 不能按预期使用转换?

我使用和发布了一个问题的答案。基于此评论,我曾经构建过答案。dplyrtidyrMap

接下来,我尝试base R仅使用工具来回答相同的问题,但这并没有按预期工作:

transform(
  df,
  Begin_New = Map(seq, Begin, End - 6000, list(by = 1000)) # or mapply(...)
)
Run Code Online (Sandbox Code Playgroud)

导致错误:

(function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : 参数暗示不同的行数: 25, 33, 84, 36, 85, 165

哦,那好吧。这似乎行不通,为什么这个行得通?

df2 <- data.frame(id = 1:4, nested = c("a, b, f", "c, d", "e", "e, f"))
transform(df2, nested = strsplit(nested, ", "))
Run Code Online (Sandbox Code Playgroud)

在我的理解中Map(seq, Begin, End - 6000, list(by = …

dictionary r list dataframe

7
推荐指数
2
解决办法
219
查看次数

How to specify random coefficients priors in rstanarm?

Suppose I have a following formula for a mixed effects model:

Performance ~ 1 + WorkingHours + Tenure + (1 + WorkingHours + Tenure || JobClass)
Run Code Online (Sandbox Code Playgroud)

then I can specify priors for fixed slopes and fixed intercept as:

prior = normal(c(mu1,mu2), c(sd1,sd2), autoscale = FALSE)
prior_intercept = normal(mean, scale, autoscale = FALSE)
Run Code Online (Sandbox Code Playgroud)

But how do I specify the priors for random slopes and intercept using

prior_covariance = decov(regularization, concentration, shape, scale)
Run Code Online (Sandbox Code Playgroud)

(or)

lkj(regularization, scale, df)
Run Code Online (Sandbox Code Playgroud)

if I know the variance …

regression r bayesian rstanarm

6
推荐指数
1
解决办法
204
查看次数

dplyr:group_by 并汇总以折叠(通过串联)包含 NA 的字符串列

我有一个相对简单的问题,但一直找不到解决方案。

假设我有以下数据集:

ID 虚拟变量 字符串1 字符串2 字符串3
1 0 汤姆 不适用 不适用
1 1 不适用 不适用
2 0 汤姆 不适用 不适用
2 1 不适用 不适用
2 0 不适用 不适用 鲍勃
3 0 史蒂夫 不适用 不适用
3 0 不适用 提米 不适用
4 0 亚历克斯 不适用 不适用

我想使用 group by 和 summarise 来得到以下内容:

ID 虚拟变量 字符串1 字符串2 字符串3
1 1 汤姆 不适用
2 1 汤姆 鲍勃
3 0 史蒂夫 提米 不适用
4 0 亚历克斯 不适用 不适用

我对“dummy_var”没有遇到任何问题,在汇总函数中使用 dummy_var …

r dplyr summarize

5
推荐指数
1
解决办法
774
查看次数

tidyr 的 unnest() 函数是否有基本 R 版本?

我已经使用了tidyverse很多,现在我对 Base R 的可能性很感兴趣。

让我们看一下这个简单的 data.frame

df <- data.frame(id = 1:4, nested = c("a, b, f", "c, d", "e", "e, f"))
Run Code Online (Sandbox Code Playgroud)

使用dplyr,stringr我们tidyr可以做

df %>% 
  mutate(nested = str_split(nested, ", ")) %>% 
  unnest(nested)
Run Code Online (Sandbox Code Playgroud)

得到(让我们忽略这tibble部分)

# A tibble: 8 x 2
     id nested
  <int> <chr> 
1     1 a     
2     1 b     
3     1 f     
4     2 c     
5     2 d     
6     3 e     
7     4 e     
8     4 f    
Run Code Online (Sandbox Code Playgroud)

现在我们想使用基础 R 工具重建这个。所以

transform(df, …
Run Code Online (Sandbox Code Playgroud)

r unnest tidyr

5
推荐指数
1
解决办法
483
查看次数

删除ggplot2中geom_ribbon图例键周围的填充

我想删除使用 geom_ribbon 创建的图例内的填充。请注意,这些答案并不能解决这个特定问题。

最小工作示例

library(ggplot2)
library(ggeffects)

fit <- lm(mpg ~ hp*disp, data= mtcars)

me <- ggeffect(fit,
               ci.lvl = .95,
               type = "fe",
               terms = c("hp", "disp"))

ggplot(data = me,
       mapping = aes(x = x, y = predicted, linetype = group)) +
  geom_line() +
  geom_ribbon(aes(ymin = conf.low,
                  ymax = conf.high),
              alpha = .5)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想要这个情节,但我希望图例看起来像这样: 在此输入图像描述

r ggplot2

5
推荐指数
1
解决办法
608
查看次数

如何将箱线图稍微向左或向右移动其原始位置?

我想在 ggplot 中的箱线图旁边绘制数据点。但是,我只能让 geom_boxplot 对象位于我的数据点之上。有没有办法可以将它们移过来以获得与此相似的图形?

带有与数据点相邻的箱线图的图

这是可重现的代码和一个 ggplot 对象(无格式),它将箱线图放在点上。(我知道我也可以使箱线图透明,但我更喜欢在数据旁边)

a = rep("a",100)
b = rep("b",100)
foo = as.data.frame(cbind(c(rep("a",100),rep("b",100)),c(rnorm(100,15,2.5),rnorm(100,17.5,3.2))))
colnames(foo) = c("series","value")
foo$series = as.factor(foo$series)
foo$value = as.numeric(foo$value)

ggplot(foo, aes(x = series, y = value))+
  geom_point(position = position_jitter(width = 0.1))+
  geom_boxplot(width = 0.25)
Run Code Online (Sandbox Code Playgroud)

ggplot 对象与数据点上的箱线图

谢谢!

r ggplot2 boxplot

5
推荐指数
1
解决办法
36
查看次数

使用 dplyr 跨多列进行变异

我正在尝试计算多列的行平均值。有人可以解释为什么下面的代码只计算代码中两个变量(var_1 和 var_13)的平均值,而不是所有 13 列的平均值吗?

df %>% 
rowwise() %>%
  mutate(varmean = mean(var_1:var_13)) -> df
Run Code Online (Sandbox Code Playgroud)

r dplyr data-wrangling

5
推荐指数
1
解决办法
620
查看次数

跨单行格式化gt表的方法?

我正在尝试使用条件格式化我的 gt 表,但有点停滞不前。我发现的大多数示例都是基于列标题的格式。

这是示例表的代码,其中第一列名为“row”,值为 1-6。我如何让 gt 表仅向第 1 行的值 > 3.0 添加颜色?这是我的第一个问题,如果我搞砸了,请道歉!

这是我希望桌子的样子。

library(tidyverse)  
library(gt) 

iris %>%   
  group_by(Species) %>%   
  slice_max(Sepal.Length, n=5) %>%   
  group_by(Species) %>% 
  mutate(row=row_number()) %>% 
  pivot_longer(-c(Species, row)) %>%
  mutate(Species = str_to_title(Species),
         name = gsub("\\.", " ", name)) %>% 
  pivot_wider(names_from=c(Species, name), values_from=value)%>% 
  gt() %>% 
  tab_spanner_delim(
    delim="_"
  )
Run Code Online (Sandbox Code Playgroud)

r dplyr tidyverse gt

4
推荐指数
1
解决办法
77
查看次数

过滤多列中的精确字符匹配

我正在使用 dplyr 来过滤包含“是”的列

df %>%
  filter(col1 == "Yes")
Run Code Online (Sandbox Code Playgroud)

如何跨多个列执行此操作?

r filter dplyr

3
推荐指数
1
解决办法
824
查看次数