小编Jon*_*ing的帖子

从查找表中更新向量的某些值的规范 tidyverse 方法

我经常需要根据查找表重新编码数据框列中的一些(不是全部!)值。我对我所知道的解决问题的方法并不满意。我希望能够以一种清晰、稳定和高效的方式做到这一点。在我编写自己的函数之前,我想确保我没有复制已经存在的标准。

## Toy example
data = data.frame(
  id = 1:7,
  x = c("A", "A", "B", "C", "D", "AA", ".")
)

lookup = data.frame(
  old = c("A", "D", "."),
  new = c("a", "d", "!")
)

## desired result
#   id  x
# 1  1  a
# 2  2  a
# 3  3  B
# 4  4  C
# 5  5  d
# 6  6 AA
# 7  7  !
Run Code Online (Sandbox Code Playgroud)

我可以通过加入、合并、取消选择来做到这一点,如下所示,但这并不像我想要的那么清楚- 步骤太多。

## This works, but …
Run Code Online (Sandbox Code Playgroud)

r dplyr recode data.table tidyverse

24
推荐指数
6
解决办法
827
查看次数

通过Rmarkdown中数据行数函数设置绘图高度等于相邻表格高度

我正在使用 mtcars 数据在 Rmarkdown 中创建并排图和表格。

---
title: "document"
author: "Maral Dorri"
date: 'May 2022'
output:
  html_document
---
Run Code Online (Sandbox Code Playgroud)

我创建一个列并在右侧添加表格:

<div class = "row">
<div class = "col-md-3">
```{r}
raw_dat <- mtcars[1:15, ] %>% rownames_to_column(var = "id") %>% select(id, mpg) %>% 
  mutate(links = paste(.$id, "And <a href = 'https://www.cars.com//'>here</a>"))
tibble(
  name = raw_dat$id,
  link = paste(raw_dat$mpg, "And <a href = 'https://www.cars.com//'>here</a>")) %>%
  mutate(link = map(link, gt::html)) %>%
  gt

```
</div>
Run Code Online (Sandbox Code Playgroud)

然后我创建另一列并打印左侧的图

<div class = "col-md-9">
```{r, fig.height=5.5}
   ggplot(raw_dat, aes(factor(id, rev(id)), mpg)) + …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 r-markdown

7
推荐指数
1
解决办法
607
查看次数

使用滑动窗口对ggplot时间序列图进行动画处理

我正在寻找在不损失分辨率的情况下为长时间序列图制作动画的方法。我希望视图在数据之间“平移”,以显示从头到尾的滑动子集。

假设我有以下内容:

  library(ggplot2)
  library(dplyr)
  library(gganimate)

  df <- as.data.frame(cumsum(rnorm(1:10000))) %>% rename(y = 1)
  df <- mutate(df, seq = seq(1, 10000, by = 1))

  ggplot(df, aes(x = seq, y = y)) + 
    geom_line()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想创建一个动画来显示更多细节,方法是一次只关注数据的一部分,然后从头到尾滑动。想象一下通过放大镜放大该系列,同时在下方滑动图...这就是我要达到的效果。有可能通过gganimate吗?如果没有,有什么建议吗?

r time-series ggplot2 gganimate

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

将 sf 几何与变换后的几何相结合

我正在用 R 复制xkcd 的“错误地图投影:ABS(经度)”。我有一个不错的开始,但我想以“正确”的方式进行。

在这里,我拿了一张世界地图并制作了一个带有水平反射的副本。然后我绘制两者并将观察窗口裁剪为正经度。

library(ggplot2)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)

world <- ne_countries(scale = "small", returnclass = "sf") |>
  sf::st_as_sf() %>% 
  sf::st_set_crs(value = 4326) 

# matrix multiplication to reflect longitude
world_rev <- st_geometry(world)  * matrix(c(-1,0,0,1), 2, 2) 
world2 <- world |>
  st_set_geometry(world_rev) |>
  sf::st_as_sf() |>
  sf::st_set_crs(value = 4326) 

ggplot() +
  geom_sf(data = world, fill = "#336666", alpha = 0.5) +
  geom_sf(data = world2, fill = "#663366", alpha = 0.5) +
  coord_sf(xlim = c(0, 180), expand = 0)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这样做的一个缺点是我似乎无法应用不同的预测。例如,如果我以

coord_sf(crs= …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 r-sf

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

如何计算多个变量的总计百分比?

students <- data.frame( names = c( "Bill", "Stacey", "Fred", "Jane", "Sarah" ), 
                        gender = c( "M", "F", "M", "F", "F" ),
                        age = c( "10-12", "10-12", "13-15", "7-9", "16-18" ),
                        stringsAsFactors = FALSE )
Run Code Online (Sandbox Code Playgroud)

我已将下面的函数用于一个变量,但是它不适用于多个变量。

library(dplyr)
result_dplyr <- students %>%
group_by(gender) %>%
summarise(per_to_tot = n() / nrow(student) * 100)
print(result_dplyr)
Run Code Online (Sandbox Code Playgroud)

所需的输出如下。

#    gender per to tot
# 1:      M 40
# 2:      F 60
#    age  
# 1:     7-9   20
# 2:     10-12 40
# 3:     13-15 20
# 4: …
Run Code Online (Sandbox Code Playgroud)

r dplyr

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

R dplyr 列作为变量

请你帮助我好吗。

我已经尝试了很多组合,但没有任何效果。

这里的例子:

library(dplyr)

foo <- function(AA,x){
  mn <- make.names(x)
  mn <- enquo(mn)
  filter(AA,mn==min(!!mn))
}

aa <- data.frame(A=c("a","b","c","d"), B.D = c(1,2,1,3))

foo(aa,"B D")

Run Code Online (Sandbox Code Playgroud)

输出是

 Error: Base operators are not defined for quosures.
Do you need to unquote the quosure?

  # Bad:
  myquosure == rhs

  # Good:
  !!myquosure == rhs
Run `rlang::last_error()` to see where the error occurred. 
Run Code Online (Sandbox Code Playgroud)

代替

filter(aa,B.D==min(B.D))

  A B.D
1 a   1
2 c   1
Run Code Online (Sandbox Code Playgroud)

您能帮我使用我的函数获得所需的输出吗?

谢谢你。

约翰

r filter dplyr

2
推荐指数
1
解决办法
50
查看次数

创建表,显示各种变量与另一个系列的排序绝对相关性

我希望输出显示每个X变量的名称,它与另一个系列的签名相关性,以及它的绝对相关性,按降序绝对相关性排序.

使用以下代码,我能够计算序列(对象res1)和X变量(位于data2数据帧内)之间的相关性.

cor(data2, res1, method = c("pearson"))
Run Code Online (Sandbox Code Playgroud)

上面的代码生成了下面的输出,它在控制台中垂直显示.

         [,1]
x1 0.45683210
x2 0.62858863
x3 0.08457911
x4 0.41022052
Run Code Online (Sandbox Code Playgroud)

接下来,使用以下代码,我可以使用sort()函数按绝对值对这些相关性进行排名.

abs(cor(data2, res1, method = c("pearson")))
abs1<-abs(cor(data2, res1, method = c("pearson")))
sort(abs1, decreasing = TRUE)
Run Code Online (Sandbox Code Playgroud)

而且,我得到了以下输出.

[1] 0.62858863 0.45683210 0.41022052 0.08457911 
Run Code Online (Sandbox Code Playgroud)

我想生成一个看起来像表或数据帧的输出.

在第一列中,您将拥有X变量的标签.
在第二列中,您将具有绝对相关性.
在第三列中,您将获得实际的相关性.

并且,此垂直表格列表将按降序排列.我想我有我需要的所有信息.我只需要代码来生成指定的输出.

sorting r ranking correlation

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