簡體   English   中英

如何在 R 中的循環中刪除重復項

[英]How to remove duplicates in a loop in R

我有一個循環,它遍歷大量 .tsv 文件並將 function 到 output 結果運行到一個文件。 循環有效,但是 .tsv 文件的副本在其中一列中有重復值,這會阻止循環工作。 我需要刪除列 V5 中具有重復值的行。 我已經嘗試過此站點上解決的先前命令,但由於某種原因它們無法正常工作..

我的 input.tsv 文件看起來像這樣(other_trait)

V1         V2         V3   V4    V5                    
10        201874235  G   T   rs389130213 

10        201876195  G   C   rs121467298 

10        201876295  T   A   rs121467298 

我的代碼開始如下格式化文件,然后運行 function。

files <- list.files(path =".", pattern = ".tsv")
files
datalist = list()
for(i in 1:length(files)) {  
  other_trait <- read.table(files[i])
  colnames(other_trait)[which(names(other_trait) == "V2")] <- "BP"
  other_trait<- merge(other_trait, subset_1[,c("BP","MAF")], by="BP")
  other_trait <- unique(other_trait$V5)

我已經嘗試使用上面的 unique 以及other_trait <- other_trait[,(duplicated(other_trait$V5)), ] Unique 刪除行 dataframe 中的其他值並且只保留 V5 中的唯一值,並且 !(duplicated) 沒有似乎什么都做!

df <- read.table(text = "V1 V2 V3 V4 V5
10 201874235 G T rs389130213

10 201876195 G C rs121467298

10 201876295 T A rs121467298", h = T)

library(dplyr)
df %>% 
  rename(BP = V2) %>% 
  left_join(subset_1[,c("BP","MAF")], by="BP") %>% 
  distinct(V5, .keep_all = T)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM