【问题标题】:How can I get the index of the rows, from a data.frame with repeated cases, after using the unique() function?使用 unique() 函数后,如何从具有重复案例的 data.frame 中获取行的索引?
【发布时间】:2020-06-16 18:14:50
【问题描述】:

我正在清理我的数据库“Visitas”,因为它是由每周来医院一次的患者组成的,所以我重复了很多次相同的主题,而我只是有兴趣将患者考虑一次。

我使用unique() 函数获取“真实”患者数量,但现在无法从原始数据库中获取。

我尝试使用这些情况创建一个向量,然后使用 which() 来获取索引,但它不起作用。

我在下面留下一些代码。

# Visitas_act: active patients who are still going to the hospital
# Visitas_mod: the initial 'Visitas' database but almost cleaned.
# codeep: patient code, identifier

Visitas_mod <- Visitas_mod[Visitas_act, ]
unique(Visitas_mod[, 'codeep'])
Visitas_r <- unique(Visitas_mod[, 'codeep'])

我试过了,但它不起作用,因为索引在“Visitas_mod”数据库中不匹配

tut <- which(Visitas_mod[, 'codeep'] == Visitas_r)    
Visitas_mod <- Visitas_mod[tut, ]

【问题讨论】:

  • 嗨@CarlosPascualBosch,我不太明白你想要做什么。因此,使用Visitas_r,您可以获得一个唯一的患者代码向量。您需要原始数据框中的什么?当右侧是向量时,您不能使用 == 。你可以使用 %in% 但是.. 我还是不明白你的逻辑
  • 已回答但感谢您的关注。我的目的是获取“Visitas_r”患者的索引,即不重复的代码患者(codeep),以便从原始数据库“Visitas_mod”中提取它们以仅与活动患者一起使用。对不起我的表达。

标签: r indexing rows


【解决方案1】:

我不完全理解你的问题,但我最好的猜测是这样的,如果你有任何问题或者我弄错了,请评论:)

#create dummy names
names <- cbind(row.names(mtcars),c(1:32))
visit <- sample(names[,1],400,replace = T)
df <- as.data.frame(x = visit)
id <- names[match(df$visit,names),2]
df <- cbind(df,id)


#get vector of unique visitors
uniqV <- unique(df$visit)
#this returns the index of first occurence of a visitor in the dataframe
match(uniqV,df$visit)

输出:

 1   2   3   5   6   7   8   9  11  12  13  14  15  20  21  23  24  25  28  30  32  37  46
47  53  58  64  68  85  90 107 120

所有位置都可以用这部分代码完成

#return all position of unique visitor in dataset
test <- function(x) {
  which(df$visit %in% x)
}

matches <- sapply(uniqV,test)

out <- cbind(as.character(uniqV), unlist(lapply(matches, paste, collapse =",")))

输出:

1   Ford Pantera L  1,98,116,127,128,136,142,189,208,210,217,254,273,275,277,298,313,360
2   Hornet Sportabout   2,19,29,78,81,112,144,234,294,303,322,332,387
3   Merc 280C   3,4,39,159,173,188,308,309,312,351,389
4   Merc 230    5,18,48,77,106,150,161,211,219,240,241,270,299,306,330,365,383
5   Dodge Challenger    6,74,154,172,222,268,336,352,367
6   Valiant 7,26,207,235,258,265,392,397
7   Porsche 914-2   8,45,102,113,117,119,121,131,149,151,224,288,329,343,350,362,394
8   Fiat X1-9   9,10,65,95,135,170,271,282,285,346,380

【讨论】:

  • 谢谢!我不知道 match() 函数,现在它可以工作了,我得到了非重复值的索引。对不起我的表情,这是我第一次来这里。
  • 没问题,这是一个有趣的问题解决,我对回答问题太陌生了,我很高兴能帮助你:)
猜你喜欢
  • 2016-07-29
  • 1970-01-01
  • 2017-12-01
  • 1970-01-01
  • 2017-01-29
  • 1970-01-01
  • 2015-12-15
  • 1970-01-01
  • 2013-04-12
相关资源
最近更新 更多