获取已删除行的索引

发布于 2025-01-17 07:26:46 字数 780 浏览 3 评论 0原文

我必须删除数据框中的一行并获取已删除行的索引。

在实践中,我单击闪亮应用程序中表格的一行,然后使用操作按钮将其删除:

从这一行我需要索引:

这是一个示例来演示我想要做什么:

library(dplyr)

df1 <- diamonds %>% 
  select(1:2) %>% 
  slice(1:5)

  carat cut    
  <dbl> <ord>  
1  0.23 Ideal  
2  0.21 Premium
3  0.23 Good   
4  0.29 Premium
5  0.31 Good

从 df1 我删除 第 3 行并获取df2。但我需要已删除行的索引作为向量。

df2 <- df1 %>% 
  slice(-3)

  carat cut    
  <dbl> <ord>  
1  0.23 Ideal  
2  0.21 Premium
3  0.29 Premium
4  0.31 Good 

我已经尝试过:

anti_join(df1, df2) %>% 
  rownames_to_column("id") %>% 
  pull(id)

给出:

[1] "1"

预期输出

[1] "3"

I have to delete a row in a dataframe and get the index of the deleted row.

In practice I click on a row of a table in a shiny app and delete it with an action button:

From this row I need the index:

Here is an example to demonstrate what I would like to do:

library(dplyr)

df1 <- diamonds %>% 
  select(1:2) %>% 
  slice(1:5)

  carat cut    
  <dbl> <ord>  
1  0.23 Ideal  
2  0.21 Premium
3  0.23 Good   
4  0.29 Premium
5  0.31 Good

From df1 I delete row 3 and get df2. But I need the index of the deleted row as vector.

df2 <- df1 %>% 
  slice(-3)

  carat cut    
  <dbl> <ord>  
1  0.23 Ideal  
2  0.21 Premium
3  0.29 Premium
4  0.31 Good 

I have tried:

anti_join(df1, df2) %>% 
  rownames_to_column("id") %>% 
  pull(id)

which gives:

[1] "1"

Expected Output

[1] "3"

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

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

发布评论

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

评论(1

一场信仰旅途 2025-01-24 07:26:46

我们可以在“df1”中创建一个序列列

df1 <- diamonds %>% 
       select(1:2) %>% 
       slice(1:5) %>%
       mutate(rn = row_number())
df2 <- df1 %>% 
  slice(-3)

,然后只需 pull rn

anti_join(df1, df2) %>% 
    pull('rn')
#[1] 3

rownames_to_column 不会给出正确的行号,因为 tibble 不允许使用行名称,并且它会重置每个子集上的行号,因此我们仅获得更改后的行号而不是原始行号。除了 tibble 情况之外,这里 slice 确实会重置,即如果我们检查其中涉及的函数,则会调用 dplyr_new_data_frame,这可能会重置行号

> methods("slice")
#[1] slice.data.frame* slice.index    
> getAnywhere("slice.data.frame")
function (.data, ..., .preserve = FALSE) 
{
    loc <- slice_rows(.data, ...)
    dplyr_row_slice(.data, loc, preserve = .preserve)
}
> dplyr:: dplyr_row_slice
function (data, i, ...) 
{
    if (!is.numeric(i) && !is.logical(i)) {
        abort("`i` must be an numeric or logical vector.")
    }
    UseMethod("dplyr_row_slice")
}
> methods("dplyr_row_slice")
[1] dplyr_row_slice.data.frame* dplyr_row_slice.grouped_df* dplyr_row_slice.rowwise_df*

> getAnywhere("dplyr_row_slice.data.frame")
function (data, i, ...) 
{
    dplyr_reconstruct(vec_slice(data, i), data)
}
> dplyr_reconstruct
function (data, template) 
{
    data <- dplyr_new_data_frame(data)
    return(dplyr_reconstruct_dispatch(data, template))
    UseMethod("dplyr_reconstruct", template)
}
> dplyr:::dplyr_new_data_frame
function (x = data.frame(), n = NULL, ..., row.names = NULL, 
    class = NULL) 
{
    row.names <- row.names %||% .row_names_info(x, type = 0L)
    new_data_frame(x, n = n, ..., row.names = row.names, class = class)
}

We could create a sequence column in 'df1'

df1 <- diamonds %>% 
       select(1:2) %>% 
       slice(1:5) %>%
       mutate(rn = row_number())
df2 <- df1 %>% 
  slice(-3)

and then just pull the rn

anti_join(df1, df2) %>% 
    pull('rn')
#[1] 3

rownames_to_column doesn't give the correct row number because tibble doesn't allow for row names and it resets the row number on each subset, thus we get only the changed row number instead of the original one. In addition to the tibble case, here the slice does reset i.e. if we check the functions involved in it, there is a call to dplyr_new_data_frame, which may be resetting the row number

> methods("slice")
#[1] slice.data.frame* slice.index    
> getAnywhere("slice.data.frame")
function (.data, ..., .preserve = FALSE) 
{
    loc <- slice_rows(.data, ...)
    dplyr_row_slice(.data, loc, preserve = .preserve)
}
> dplyr:: dplyr_row_slice
function (data, i, ...) 
{
    if (!is.numeric(i) && !is.logical(i)) {
        abort("`i` must be an numeric or logical vector.")
    }
    UseMethod("dplyr_row_slice")
}
> methods("dplyr_row_slice")
[1] dplyr_row_slice.data.frame* dplyr_row_slice.grouped_df* dplyr_row_slice.rowwise_df*

> getAnywhere("dplyr_row_slice.data.frame")
function (data, i, ...) 
{
    dplyr_reconstruct(vec_slice(data, i), data)
}
> dplyr_reconstruct
function (data, template) 
{
    data <- dplyr_new_data_frame(data)
    return(dplyr_reconstruct_dispatch(data, template))
    UseMethod("dplyr_reconstruct", template)
}
> dplyr:::dplyr_new_data_frame
function (x = data.frame(), n = NULL, ..., row.names = NULL, 
    class = NULL) 
{
    row.names <- row.names %||% .row_names_info(x, type = 0L)
    new_data_frame(x, n = n, ..., row.names = row.names, class = class)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文