[英]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 每个ID
的index
行。
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.