簡體   English   中英

R 使用另一個列中的值索引數據框

[英]R indexing a data frame using values in the column of another

我有一個數據框,其中兩列是另一個數據名的索引。 我想通過索引第二列向第一列添加一列,但僅調用列名是行不通的。 例如,如果第一個數據幀是:

...  Gene    CellLine ...
     KRAS    HELA     ...
     BRCA1   T24      ...

我的第二個 dataframe 看起來像

        KRAS   BRCA1 ...
HELA    5      3
T24     2      1
...

我希望 output 看起來像

...  Gene   CellLine   Dependency ...
     KRAS   HELA       5          ...
     BRCA1  T24        1          ...

無需遍歷線路,因為第一個數據幀很大。 也就是說,是否有任何 function 或 package 相當於

for (i in rownames(table1)){
  table1[i, dependency] <- ifelse(table1[i,"Gene"] %in% rownames(table2) & table1[i,"CellLine"] %in% colnames(table2), table2[table1[i,"Gene"],table1[i,"CellLine"]], NA)
}

但更快?

謝謝!

以下代碼是矢量化的,它使用df1的兩列創建一個索引矩陣,並使用它從df2中提取所需的值。

inx <- as.matrix(df1[c("CellLine", "Gene")])
df1$Dependency <- df2[inx]

df1
#   Gene CellLine Dependency
#1  KRAS     HELA          5
#2 BRCA1      T24          1

數據

df1 <- read.table(text = "
Gene    CellLine 
KRAS    HELA
BRCA1   T24 
", header = TRUE)

df2 <- read.table(text = "
        KRAS   BRCA1
HELA    5      3
T24     2      1
", header = TRUE)

你可以試試這個方法。 使用的數據如下:

#Data
df1 <- structure(list(Gene = c("KRAS", "BRCA1"), CellLine = c("HELA", 
"T24")), class = "data.frame", row.names = c(NA, -2L))
df2 <- structure(list(id = c("HELA", "T24"), KRAS = c(5L, 2L), BRCA1 = c(3L, 
1L)), class = "data.frame", row.names = c(NA, -2L))

然后代碼,就可以meltmerge數據了:

library(reshape)
#Melt df2 
Melted <- melt(df2,id.vars = 'id')
#Now merge
Merged <- merge(df1,Melted,by.x=c('Gene','CellLine'),by.y=c('variable','id'),all.x=T)

結果將是下一個:

   Gene CellLine value
1 BRCA1      T24     1
2  KRAS     HELA     5

暫無
暫無

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

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