2016-09-25 62 views
0

我想根據一列中的值從數據中刪除某些行。我已經嘗試了幾種方法:根據列對子數據框進行子集

#reads in data 
sbc016formants.df <- read.table("file path", sep="\t", header = F, strip.white = T) 

# names columns 
names(sbc016formants.df) <- c("fileName", "start", "end", "vowelLabel") 

# list of values I want to remove 
list16 <- c(615.162, 775.885) 

# produces a subset of data - removes rows with values from list 16 in the start column 
sbc016formants.df <- subset(sbc016formants.df, !start %in% list16) 

產生此錯誤消息一些,但不是所有的我的數據文件:

Error in match(x, table, nomatch = 0L) : 
'match' requires vector arguments 

我也試過這樣的基礎上,第二個答案中this話題

sbc002formants.df <- sbc002formants.df[ apply(sbc002formants.df, 1 , function(x) any(unlist(x) %in% list2)) , ] 

而這樣做會清除列表中的一些項目(list16),但不是全部。我想使用第一個答案,但我不明白代碼(在示例中,我不確定bl是什麼)。

這裏是做重複的例子代碼:

# creates dataframe 
fileName <- c("sbc016", "sbc016", "sbc016", "sbc016") 
start <- c(1.345, 2.345, 615.162, 775.885) 
end <- c(100.345, 200.345, 715.162, 875.885) 
sbc016formants.df <- data.frame(fileName, start, end) 

# list of what I want to get rid of 
list16 <- c(615.162, 775.885) 
+0

試試'sbc016formants.df [!(在%list16 sbc016formants.df $開頭%)]'? – aichao

+0

我試圖重現錯誤,但我沒有收到錯誤消息 – Pieter

+0

@aichao,這不會產生任何錯誤消息,但它也不會執行子集化。 – Lisa

回答

1

。假定我理解正確的問題,dplyr應該能夠輕鬆高效地做到這一點。

fileName <- c("sbc016", "sbc016", "sbc016", "sbc016") 
start <- c(1.345, 2.345, 615.162, 775.885) 
end <- c(100.345, 200.345, 715.162, 875.885) 
sbc016formants.df <- data.frame(fileName, start, end) 

# list of what I want to get rid of 
list16 <- c(615.162, 775.885) 

install.packages("dplyr", dependencies = TRUE) 
library(dplyr) 
sbc016formants.df %>% filter(!start %in% list16) 

sbc016formants.df %>% filter(start != list16) 
+0

這確實有效,但我仍然不確定爲什麼以前的解決方案失敗。謝謝! – Lisa

相關問題