数据框中的数字向量的字符值

发布于 2025-02-13 08:50:22 字数 2090 浏览 0 评论 0原文

从数据框中,我获得了列的字符向量: 按日期,时间和group_by日期安排

fall_hc %>% arrange(a_dat,AZeit) %>% group_by(a_dat) %>%
        mutate(time_vec = str_c(snzeit,collapse= ",")) %>% 
        ungroup() %>% 
        filter(!is.na(time_vec))

此符号向量,例如:

x <- c("5, 31, 16, 64, 9, 10, 31") 

我需要在dataframe中的x中的数字向量: 5 31 16 64 9 10 31.
因为我想计算diff(x):26 -15 48 -55 1 21.
并进一步处理这以计算否定差异的数量。

以下是一些数据:

tibble::tribble(
              ~a_dat,  ~AZeit, ~snzeit,
        "2019-01-02", "24180",      31,
        "2019-01-02", "24360",      27,
        "2019-01-02", "24480",      16,
        "2019-01-02", "24780",      64,
        "2019-01-02", "30420",       9,
        "2019-01-02", "30840",      10,
        "2019-01-02", "35280",      31,
        "2019-01-03", "24120",      40,
        "2019-01-03", "24120",      27,
        "2019-01-03", "24480",       6,
        "2019-01-03", "24480",       4,
        "2019-01-03", "24780",       9,
        "2019-01-03", "25380",      25,
        "2019-01-03", "26460",      33,
        "2019-01-04", "24000",       5,
        "2019-01-04", "24360",       2,
        "2019-01-04", "24900",       1,
        "2019-01-04", "27180",      29,
        "2019-01-04", "30600",       8,
        "2019-01-07", "24780",      25,
        "2019-01-07", "24840",       4,
        "2019-01-07", "28920",       3,
        "2019-01-07", "31620",      11,
        "2019-01-08", "24060",      46,
        "2019-01-08", "24480",       7,
        "2019-01-08", "25260",       4,
        "2019-01-08", "27900",       5,
        "2019-01-08", "29820",       5,
        "2019-01-08", "30060",      74,
        "2019-01-08", "33360",       5,
        "2019-01-08", "33600",      28,
        "2019-01-08", "34200",      15,
        "2019-01-08", "35520",      13,
        "2019-01-08", "36000",      19,
        "2019-01-08", "44100",      24
        )

它们已经对其进行分类,因此您可以删除%&gt;%bepand(a_dat,azeit)。

为了澄清目的: 我需要知道如何对Snzeit进行分类,这是从切割到缝合线的时间。想确定从短到长时间分类的那些日子。

From a dataframe I get a character vector of a column by:
arrange by date, time and group_by date

fall_hc %>% arrange(a_dat,AZeit) %>% group_by(a_dat) %>%
        mutate(time_vec = str_c(snzeit,collapse= ",")) %>% 
        ungroup() %>% 
        filter(!is.na(time_vec))

This results in character vectors like:

x <- c("5, 31, 16, 64, 9, 10, 31") 

I need a numeric vector from this x within a dataframe:
5 31 16 64 9 10 31.
because I want to calculate diff(x): 26 -15 48 -55 1 21.
and further process this to calculate the number of negativ differences.

Here are some data:

tibble::tribble(
              ~a_dat,  ~AZeit, ~snzeit,
        "2019-01-02", "24180",      31,
        "2019-01-02", "24360",      27,
        "2019-01-02", "24480",      16,
        "2019-01-02", "24780",      64,
        "2019-01-02", "30420",       9,
        "2019-01-02", "30840",      10,
        "2019-01-02", "35280",      31,
        "2019-01-03", "24120",      40,
        "2019-01-03", "24120",      27,
        "2019-01-03", "24480",       6,
        "2019-01-03", "24480",       4,
        "2019-01-03", "24780",       9,
        "2019-01-03", "25380",      25,
        "2019-01-03", "26460",      33,
        "2019-01-04", "24000",       5,
        "2019-01-04", "24360",       2,
        "2019-01-04", "24900",       1,
        "2019-01-04", "27180",      29,
        "2019-01-04", "30600",       8,
        "2019-01-07", "24780",      25,
        "2019-01-07", "24840",       4,
        "2019-01-07", "28920",       3,
        "2019-01-07", "31620",      11,
        "2019-01-08", "24060",      46,
        "2019-01-08", "24480",       7,
        "2019-01-08", "25260",       4,
        "2019-01-08", "27900",       5,
        "2019-01-08", "29820",       5,
        "2019-01-08", "30060",      74,
        "2019-01-08", "33360",       5,
        "2019-01-08", "33600",      28,
        "2019-01-08", "34200",      15,
        "2019-01-08", "35520",      13,
        "2019-01-08", "36000",      19,
        "2019-01-08", "44100",      24
        )

They are already sorted, so you can leave out %>% arrange(a_dat,AZeit).

To clarify the purpose:
I need to know how the operations are sorted with respect to snzeit, thats the time from cut to suture. Want to identify those days where they are sorted from short to long.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我太投入 2025-02-20 08:50:22

如果我正确理解了您的问题,您需要在第一名中创建该字符向量,您只需要列表柱子,然后在每个列表上应用diff并计算每个a_dat


library(dplyr)
library(tibble)


fall_hc <- tibble::tribble(
  ~a_dat,  ~AZeit, ~snzeit,
  "2019-01-02", "24180",      31,
  "2019-01-02", "24360",      27,
  "2019-01-02", "24480",      16,
  "2019-01-02", "24780",      64,
  "2019-01-02", "30420",       9,
  "2019-01-02", "30840",      10,
  "2019-01-02", "35280",      31,
  "2019-01-03", "24120",      40,
  "2019-01-03", "24120",      27,
  "2019-01-03", "24480",       6,
  "2019-01-03", "24480",       4,
  "2019-01-03", "24780",       9,
  "2019-01-03", "25380",      25,
  "2019-01-03", "26460",      33,
  "2019-01-04", "24000",       5,
  "2019-01-04", "24360",       2,
  "2019-01-04", "24900",       1,
  "2019-01-04", "27180",      29,
  "2019-01-04", "30600",       8,
  "2019-01-07", "24780",      25,
  "2019-01-07", "24840",       4,
  "2019-01-07", "28920",       3,
  "2019-01-07", "31620",      11,
  "2019-01-08", "24060",      46,
  "2019-01-08", "24480",       7,
  "2019-01-08", "25260",       4,
  "2019-01-08", "27900",       5,
  "2019-01-08", "29820",       5,
  "2019-01-08", "30060",      74,
  "2019-01-08", "33360",       5,
  "2019-01-08", "33600",      28,
  "2019-01-08", "34200",      15,
  "2019-01-08", "35520",      13,
  "2019-01-08", "36000",      19,
  "2019-01-08", "44100",      24
)


fall_hc %>%
    arrange(a_dat, AZeit) %>%
    group_by(a_dat) %>%
    summarise(
        time_vec = list(snzeit)
    ) %>%
    group_by(a_dat) %>%
    summarise(
        time_vec = diff(unlist(time_vec)) # caluculate diffs for each list
    ) %>%
    group_by(a_dat) %>%
    summarise(
        time_vec_neg = sum(time_vec < 0) # count number of negative values
    )

#> `summarise()` has grouped output by 'a_dat'. You can override using the
#> `.groups` argument.
#> # A tibble: 5 × 2
#>   a_dat      time_vec_neg
#>   <chr>             <int>
#> 1 2019-01-02            3
#> 2 2019-01-03            3
#> 3 2019-01-04            3
#> 4 2019-01-07            2
#> 5 2019-01-08            5

reprex软件包(v2.0.1)

If I have understood your problem correctly, you donot need to create that character vector in the 1st place, you just need list-column and then applying diff on each list and calculate the number of negative values for each a_dat


library(dplyr)
library(tibble)


fall_hc <- tibble::tribble(
  ~a_dat,  ~AZeit, ~snzeit,
  "2019-01-02", "24180",      31,
  "2019-01-02", "24360",      27,
  "2019-01-02", "24480",      16,
  "2019-01-02", "24780",      64,
  "2019-01-02", "30420",       9,
  "2019-01-02", "30840",      10,
  "2019-01-02", "35280",      31,
  "2019-01-03", "24120",      40,
  "2019-01-03", "24120",      27,
  "2019-01-03", "24480",       6,
  "2019-01-03", "24480",       4,
  "2019-01-03", "24780",       9,
  "2019-01-03", "25380",      25,
  "2019-01-03", "26460",      33,
  "2019-01-04", "24000",       5,
  "2019-01-04", "24360",       2,
  "2019-01-04", "24900",       1,
  "2019-01-04", "27180",      29,
  "2019-01-04", "30600",       8,
  "2019-01-07", "24780",      25,
  "2019-01-07", "24840",       4,
  "2019-01-07", "28920",       3,
  "2019-01-07", "31620",      11,
  "2019-01-08", "24060",      46,
  "2019-01-08", "24480",       7,
  "2019-01-08", "25260",       4,
  "2019-01-08", "27900",       5,
  "2019-01-08", "29820",       5,
  "2019-01-08", "30060",      74,
  "2019-01-08", "33360",       5,
  "2019-01-08", "33600",      28,
  "2019-01-08", "34200",      15,
  "2019-01-08", "35520",      13,
  "2019-01-08", "36000",      19,
  "2019-01-08", "44100",      24
)


fall_hc %>%
    arrange(a_dat, AZeit) %>%
    group_by(a_dat) %>%
    summarise(
        time_vec = list(snzeit)
    ) %>%
    group_by(a_dat) %>%
    summarise(
        time_vec = diff(unlist(time_vec)) # caluculate diffs for each list
    ) %>%
    group_by(a_dat) %>%
    summarise(
        time_vec_neg = sum(time_vec < 0) # count number of negative values
    )

#> `summarise()` has grouped output by 'a_dat'. You can override using the
#> `.groups` argument.
#> # A tibble: 5 × 2
#>   a_dat      time_vec_neg
#>   <chr>             <int>
#> 1 2019-01-02            3
#> 2 2019-01-03            3
#> 3 2019-01-04            3
#> 4 2019-01-07            2
#> 5 2019-01-08            5

Created on 2022-07-07 by the reprex package (v2.0.1)

无人接听 2025-02-20 08:50:22

list 中将数字保留为 n数字

mtcars %>% 
  group_by(cyl) %>% 
  summarise(x = list(mpg))
# # A tibble: 3 x 2
#         cyl x         
#      <dbl> <list>    
#    1     4 <dbl [11]>
#    2     6 <dbl [7]> 
#    3     8 <dbl [14]>

然后,我们可以在下面的数字列表中进行:

mtcars %>% 
  group_by(cyl) %>% 
  summarise(x = list(mpg)) %>% 
  group_by(cyl) %>% 
  summarise(xDiffSum = sum(diff(unlist(x))))
# # A tibble: 3 x 2
#      cyl xDiffSum
#   <dbl>    <dbl>
# 1     4    -1.40
# 2     6    -1.3 
# 3     8    -3.7 

Keep numbers as numeric in a list:

mtcars %>% 
  group_by(cyl) %>% 
  summarise(x = list(mpg))
# # A tibble: 3 x 2
#         cyl x         
#      <dbl> <list>    
#    1     4 <dbl [11]>
#    2     6 <dbl [7]> 
#    3     8 <dbl [14]>

Then we can do below on the list of numbers:

mtcars %>% 
  group_by(cyl) %>% 
  summarise(x = list(mpg)) %>% 
  group_by(cyl) %>% 
  summarise(xDiffSum = sum(diff(unlist(x))))
# # A tibble: 3 x 2
#      cyl xDiffSum
#   <dbl>    <dbl>
# 1     4    -1.40
# 2     6    -1.3 
# 3     8    -3.7 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文