![](/img/trans.png)
[英]Pulling data from MySQL database and pulling only 1 value of each in a column and then show latest first
[英]Pulling data from a dataframe column only if it contains a certain value
對於R語言中的編程來說,這是一個全新的知識,我有一個數據框,我試圖通過僅通過在“名稱”列中包含特定名稱的情況下拉整整行來創建更簡潔的表。 名稱全都在單獨的文本文檔中。 有什么建議么?
我試過了:
refGenestable <- dbGetQuery(con, "select row_names, name, chrom, strand, txStart, txEnd from refGene where name in c_Gene")
其中c_Gene是我需要測試已變成數據幀的名稱列表。 我也嘗試過將其轉換成字符串列表並進行迭代,但是也遇到了問題
編輯:對不起,我還在學習混亂! 我在R中創建了數據框(“ refGenestable”)(但是的,它來自SQL數據庫),但現在我希望將其范圍縮小到僅包含與我在文本文件c_Genes中具有相同名稱的行,其中每個名稱以\\ n分隔。 我從該文件創建了一個列表
您可能在這里遇到一些問題。 很難確切知道您需要什么,因為不清楚數據的結構是什么。
一般問題很容易回答。
如果您有一個數據框,並且想要一個僅包含向量中名稱的新框架,則可以使用DF[DF$name %in% <some vector>)
或dplyr filter(DF, name %in% <some vector>)
。 但是,您不能使用%in%
來測試數據中是否包含某些內容。 您實際上必須在另一個數據框中提取變量。
如果要保留的名稱是文本文件中的行,那么您還會問一個有關如何將文本文件放入R的問題,在這種情況下,它是my_vector <- readLines("path to file")
。 實際的代碼將取決於文件的結構,但是如果每個元素都在換行符上,則將執行您想要的操作。
如果要保留的名稱在另一個數據框中,則需要將它們提取為矢量,以便使用%in%
,即filter(DF, name, name %in% OTHERDF$name)
編輯:從您的編輯到問題,我的答案可能對您有用。 但是,同樣,我們不確定是否看不到數據的結構(可以通過粘貼dput(<your object>)
的輸出來提供數據。這是上面的答案,使用對象的名稱你已經描述過了
gene_names <- readLines("c_Genes")
# is that really the name? No extension? Is it in your working directory?
# if not, you need to use a relative or absolute path for c_Genes
genes_you_want <- refGenestable[refGenestable$name %in% gene_names,]
# is the column with the gene name called name?
# don't forget the comma at the end
# or with dplyr
install.packages("dplyr")
library(dplyr)
genes_you_want <- filter(refGenestable, name %in% gene_names)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.