简体   繁体   English

当该行的行号等于R中重复的另一列的值时,如何选择一行?

[英]how to choose a row when the row number of that row is equal to the value of the other column with duplicates in R?

I have a data frame as follows -我有一个数据框如下 -

df <- cbind(c(1,1,1,2,2,2,3,3,3,3), c(6,12,18,3,9,12,4,8,12,16),c(3,3,3,2,2,2,4,4,4,4))
colnames(df) <- c("ID","value","index")

I want to get the following result -我想得到以下结果 -

df1 <- cbind(c(1,2,3), c(18,9,16),c(3,2,4))

So I basically want to extract (for each ID) the row whose row number is equal to the index for that ID.所以我基本上想提取(对于每个 ID)行号等于该 ID 的索引的行。 For example, the 3rd row for ID 1, 2nd row for ID 2 and 4th row for ID 4.例如,ID 1 的第 3 行,ID 2 的第 2 行和 ID 4 的第 4 行。

I tried the following code我尝试了以下代码

df1 <- df%>%group_by(ID)%>%filter(index==index)

But it is not working.但它不起作用。 Please help me to solve this problem.请帮我解决这个问题。

Use slice to select the index row for each ID .使用slice到 select 每个IDindex行。

library(dplyr)
df %>% group_by(ID) %>% slice(first(index)) %>% ungroup

#     ID value index
#  <dbl> <dbl> <dbl>
#1     1    18     3
#2     2     9     2
#3     3    16     4

This can be written in data.table and base R as:这可以写在data.table和基础 R 中:

library(data.table)
setDT(df)[, .SD[first(index)], ID]

#Base R
subset(df, index == ave(value, ID, FUN = seq_along))

data数据

df <- data.frame(ID = c(1,1,1,2,2,2,3,3,3,3), 
                 value = c(6,12,18,3,9,12,4,8,12,16),
                 index = c(3,3,3,2,2,2,4,4,4,4))

Just adding to the Ronak Shah's answer, I guess one of the straightforward codes to do what you want is the following:只是添加到 Ronak Shah 的答案中,我想执行您想要的操作的简单代码之一如下:

library(dplyr)
df <- 
    data.frame(ID = c(1,1,1,2,2,2,3,3,3,3), value = c(6,12,18,3,9,12,4,8,12,16), index = c(3,3,3,2,2,2,4,4,4,4))

df %>% group_by(ID) %>% filter(row_number() == index) %>% ungroup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R,我怎么知道这个矩阵的行数等于另一个矩阵的第一个值? - R, how do i know the number of row that this matrix has equal to the first value of this other matrix? 如何按行选择数量最多的列 - How to choose column with the largest number by row 如何在 R 中创建一行,每个行元素等于其列中的值数? - How do I create a row in R with each row element equal to the number of values in its column? 如果一行在R列中共享另一行的值并且在R中的另一列中具有一个值,如何删除该行? - How to delete a row if it shares the value of another row in one column and has one value in other column in R? 检测一行是否等于一,然后插入另一列的值 - Detect if a row is equal to one and insert the value of other column r如果值等于X,则保留行名和列名 - r keep row and column names if value equal X 在R中的条件下创建行,导致列值的平均值相等 - create row resulting in mean of column value equal under condition in R R - 选择连续出现的数字 - R - choose the number which appears most in a row 如何在 R 的列中添加第一个数字 - How to add first number in row in a column in R R:如果另一列中的行缺少值,如何提取列中的值名称和另一列的列名 - R: How to extract value name in a column and column name of another column if the row in the other column has a missing value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM