按非唯一ID选择数据框的子集

时间:2021-07-08 14:58:48

Suppose I have a dataframe like this one:

假设我有一个像这样的数据帧:

df <- data.frame (id = c("a", "b", "a", "c", "e", "d", "e"), n=1:7)

and a vector with ids like this one:

和像这样的id的向量:

v <- c("a", "b")

How can I select the rows of the dataframe that match the ids in v? I can't use the id column for rownames because they are not unique. When I try that, I get:

如何选择与v中的id匹配的数据帧行?我不能将id列用于rownames,因为它们不是唯一的。当我尝试时,我得到:

 rownames(df) <- df[["id"]]
Error in `row.names<-.data.frame`(`*tmp*`, value = c(1L, 2L, 1L, 3L, 5L,  : 
  duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': ‘a’, ‘e’ 

2 个解决方案

#1


11  

This should do what you want:

这应该做你想要的:

ndx = which(df$id %in% v)
df[ndx,]

#2


15  

Use

df[df$id %in% v,]

#1


11  

This should do what you want:

这应该做你想要的:

ndx = which(df$id %in% v)
df[ndx,]

#2


15  

Use

df[df$id %in% v,]