小编Tar*_*Jae的帖子

rownames_to_column 在 rowwise() 正确后不起作用

我有这个 df:

df <- structure(list(a = 1:5, b = 6:10, c = 11:15, d = c("a", "b", 
"c", "d", "e"), e = 1:5), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

      a     b     c d         e
  <int> <int> <int> <chr> <int>
1     1     6    11 a         1
2     2     7    12 b         2
3     3     8    13 c         3
4     4     9    14 d         4
5     5    10    15 e         5
Run Code Online (Sandbox Code Playgroud)

rownames_to_column 作品:

df %>% 
  column_to_rownames(var="d") %>% 
  rownames_to_column(var= "d") …
Run Code Online (Sandbox Code Playgroud)

r rowname dplyr rowwise tibble

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

使用 tidyverse 重新定位行

是否有可能relocate tidyverse框架像它可能与列dplyr relocate

在这个例子中,我想将第 1 行重新定位到位置 5(数据帧的结尾)

我的数据框:

df <- structure(list(ID = c(1, 2, 3, 4, 5), var1 = c("a", "b", "c", 
"d", "e"), var2 = c(1, 1, 0, 0, 1)), class = "data.frame", row.names = c(NA, 
-5L))

df
  ID var1 var2
1  1    a    1
2  2    b    1
3  3    c    0
4  4    d    0
5  5    e    1
Run Code Online (Sandbox Code Playgroud)

期望的输出:

  ID var1 var2
1  2    b    1
2  3    c    0 …
Run Code Online (Sandbox Code Playgroud)

r dplyr tidyr tidyverse relocate

4
推荐指数
2
解决办法
97
查看次数

如何在一个动画循环后停止 gganimate

是否可以在动画循环结束后停止?

动画应该执行到去年 2021 年,然后停止显示所有条形!

以下是带有 的条形动画示例gganimate

library(tidyverse)
library(gganimate)

year <- (2000:2021)
something <- mtcars$mpg[1:22]

df <- tibble(year,something)

p <- ggplot(df, aes(year, something))+
    geom_col() +
    geom_text(aes(label=something), vjust=-0.3, size=4)+
    transition_states(year, transition_length = 5, state_length = 10) + shadow_mark() +
    enter_grow() + 
    exit_fade(alpha = 1)+
    labs(title = 'Year: {closest_state}',  
         subtitle  =  "Blabla",
         caption  = "bla")

animate(p, fps=8)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

r ggplot2 gganimate

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

在R中使用pivot_longer旋转多组列

我想在数据框中旋转多组变量。我的数据如下所示:

require(dplyr)
require(tidyr)
x_1=rnorm(10,0,1)
x_2=rnorm(10,0,1)
x_3=rnorm(10,0,1)
y_1=rnorm(10,0,1)
y_2=rnorm(10,0,1)
aid=rep(1:5,2)
data=data.frame(aid, x_1,x_2,y_1,y_2)

> data
   aid         x_1        x_2         y_1         y_2
1    1 -0.82305819  0.9366731  0.95419200  2.29544019
2    2  0.64424320 -0.2807793  0.51303834  0.02560463
3    3 -1.11108822 -0.2475625  0.05747951 -0.51218368
4    4 -1.04026895 -0.4138653  0.57751999  0.60942652
5    5  1.29097040 -1.7829966  1.59940532  0.75868562
6    1 -0.57845406 -1.0002074  0.04302291  0.86766265
7    2  0.08996163 -0.7949632 -2.10422124 -0.43432995
8    3  0.14331978  0.4203010 -1.12748270  0.14484670
9    4 -0.25207187  1.5559295  0.23621422 -0.04719046
10   5 -0.25617731  0.6241852 -1.21131110  1.02236458
Run Code Online (Sandbox Code Playgroud)

我想分别旋转 x …

pivot r dplyr tidyr tidyverse

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

How to transform this base R code to dplyr using across and .names arguement

I have this dataframe:

df <- structure(list(A = c(2L, 3L, 4L, 5L, 5L), B = c(3L, 1L, 2L, 5L, 
5L), C = c(4L, 5L, 2L, 1L, 1L), D = c(3L, 1L, 5L, 1L, 2L)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -5L))

      A     B     C     D
  <int> <int> <int> <int>
1     2     3     4     3
2     3     1     5     1
3     4     2     2     5
4     5     5     1     1
5     5     5     1     2
Run Code Online (Sandbox Code Playgroud)

I want to substract …

r dplyr across

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

如何将内联代码添加到#| 无花果帽:四开本

更新:

\n

@harre。我遇到了一个问题。

\n

它与数据集完美配合mtcars

\n

但是当我尝试使用加载的数据框时,它说:

\n
Fehler in nrow(df_xxx) : Objekt \'df_xxx\' nicht gefunden\nRuft auf: paste -> nrow\nFehler in yaml::yaml.load(meta, eval.expr = TRUE) : \n  Could not evaluate expression: paste(nrow(df_xxx), "blabla")\nRuft auf: .main ... FUN -> parse_block -> partition_chunk -> <Anonymous>\nAusf\xef\xbf\xbdhrung angehalten\n
Run Code Online (Sandbox Code Playgroud)\n

#|...在这种情况下,四开图看起来首先df_xxx没有生成。尽管它在我们无法访问的环境中。\n知道吗?

\n

原始问题: \n这是一个有效的四开示例:

\n
---\ntitle: "test"\nformat: revealjs\n---\n\n## Quarto example\n\n\n```{r}\n#| label: fig-plot_mtcars\n#| fig-cap: nrow(mtcars).\n\nplot(mtcars$mpg, mtcars$disp)\n```\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n

在此输入图像描述

\n

如何将内联代码添加到fig.cap: \n所需的输出: \n在此输入图像描述

\n

我尝试过#| fig-cap: paste("bla", …

r r-markdown quarto

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

在闪亮的应用程序中使用 sliderInput 为区域着色

我有一个改编自网络的小示例应用程序:

library( shiny )
library( shinyWidgets )

  ui <- fluidPage(
    
    tags$br(),
    
    noUiSliderInput(
      inputId = "noui2", label = "Slider vertical:",
      min = 0, max = 1000, step = 50,
      value = c(100, 400), margin = 100,
      orientation = "vertical",
      width = "100px", height = "300px"
    ),
    verbatimTextOutput(outputId = "res2")
    
  )
  
  server <- function(input, output, session) {
    
    output$res2 <- renderPrint(input$noui2)
    
  }
  
  shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

然后我加载图像作为背景,如下所示:

在此输入图像描述

我想知道是否有一种方法可以对图形边框中 100 到 400 之间的特定区域高度(由滑块给出)进行着色,如下所示:

在此输入图像描述

graphics r slider background-color shiny

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

如何从 png 图像的一行获取 xy 坐标并在闪亮的应用程序中使用它

这是此Color an area with a sliderInput in a闪亮应用程序的后续问题

假设我有这个图像:

在此输入图像描述

我如何将@ismirsehregal 的解决方案应用于这张图片。我想我必须把食道的x和y放到代码中,但我不知道如何获取食道的x和y(图中绿色):

来自在闪亮的应用程序中使用滑块输入对区域进行着色的代码

library(shiny)
library(plotly)
library(shinyWidgets)

DF <- data.frame(
    x = c(cos(seq(0.01, 10, 0.01)) * 1000:1 + 1000, cos(seq(0.01, 10, 0.01)) * 1000:1 + 1500),
    y = rep(1:1000, 2),
    id = c(rep("trace_1", 1000), rep("trace_2", 1000))
  )

ui <- fluidPage(
  br(),
  column(
    2,
    noUiSliderInput(
      inputId = "noui2",
      label = "Slider vertical:",
      min = 0,
      max = 1000,
      step = 50,
      value = c(100, 400),
      margin = 100,
      orientation = "vertical", …
Run Code Online (Sandbox Code Playgroud)

png r image slider shiny

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

用于替换 dyplr 工作流程中字符串内括号内的非数字字符的正则表达式

我的问题在某种程度上与一个已经回答的问题需要使用 R 从字符串列中提取单个字符有关

我尝试用我的知识解决这个问题,并且需要知道如何删除字符串中括号中的非数字字符:`

这是带有列的数据框x

  team     linescore     ondate                                     x
1  NYM     010000000 2020-08-01             0, 1, 0, 0, 0, 0, 0, 0, 0
2  NYM (10)1140006x) 2020-08-02 (, 1, 0, ), 1, 1, 4, 0, 0, 0, 6, x, )
3  BOS     002200010 2020-08-13             0, 0, 2, 2, 0, 0, 0, 1, 0
4  NYM  00000(11)01x 2020-08-15    0, 0, 0, 0, 0, (, 1, 1, ), 0, 1, x
5  BOS        311200 2020-08-20                      3, 1, 1, 2, …
Run Code Online (Sandbox Code Playgroud)

regex r dplyr

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

字符串中有多少个数字是连续的

有这个向量:

vector <- c("236", "234", "", "12", "24", "3")

[1] "236" "234" ""    "12"  "24"  "3"
Run Code Online (Sandbox Code Playgroud)

我想检查每个元素中有多少个连续数字。

预期输出:

2 3 0 2 0 0
Run Code Online (Sandbox Code Playgroud)

我不知道该怎么做!

numbers r sequence

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