基于与不同数据帧的匹配项在数据帧中创建新的布尔列时,"replacement has x rows, data has y"出现 R 错误



我有以下两个数据帧(针对问题进行了简化(:

nsPre(较小的数据帧(:

gene_id_name
ENSG00000005020|SKAP2
ENSG00000017260|ATP2C1
ENSG00000027869|SH2D2A
ENSG00000039319|ZFYVE16
.
.
.

g0(较大的数据帧(:

gene_id_name            pLI       g_eGenes_general
ENSG00000005020|SKAP2   0.00823   0
ENSG00000039319|ZFYVE16 0.12104   0
ENSG00000087884|AAMDC   0.13539   1
ENSG00000027869|SH2D2A  0.002489  1
ENSG00000124608|AARS2   0.32500   0
.
.
.

我想做的是在g0中创建一个名为g_eGenes_nsPre的新列,每当gene_id_name值匹配时,该列就会分配一个值1,当它们不匹配时,则分配一个0,如下所示:

期望结果:

gene_id_name            pLI      g_eGenes_general   g_eGenes_nsPre
ENSG00000005020|SKAP2   0.00823  0                  1
ENSG00000039319|ZFYVE16 0.12104  0                  1
ENSG00000087884|AAMDC   0.13539  1                  0
ENSG00000027869|SH2D2A  0.002489 1                  1
ENSG00000124608|AARS2   0.32500  0                  0

当前尝试次数:

df = g0
df <- na.omit(df)    
df$g_eGenes_nsPre <- ifelse(nsPre$gene_id_name %in% g0$gene_id_name, 1, 0)
df$g_eGenes_nsPost <- ifelse(nsPost$gene_id_name %in% g0$gene_id_name, 1, 0)

运行上述代码时出错:

Error in `$<-.data.frame`(`*tmp*`, g_eGenes_nsPre, value = c(1, 1, 1,  : 
replacement has 2039 rows, data has 15430

在基本R 中

transform(g0, g_eGenes_nsPre = apply(g0, 1, function(x)
as.integer(x["gene_id_name"] %in% nsPre$gene_id_name)))
#             gene_id_name      pLI g_eGenes_general g_eGenes_nsPre
#1   ENSG00000005020|SKAP2 0.008230                0              1
#2 ENSG00000039319|ZFYVE16 0.121040                0              1
#3   ENSG00000087884|AAMDC 0.135390                1              0
#4  ENSG00000027869|SH2D2A 0.002489                1              1
#5   ENSG00000124608|AARS2 0.325000                0              0

代替as.integer,您还可以使用一元+运算符。

或使用dplyr

library(dplyr)
g0 %>%
mutate(g_eGenes_nsPre = +(gene_id_name %in% nsPre$gene_id_name))
#             gene_id_name      pLI g_eGenes_general g_eGenes_nsPre
#1   ENSG00000005020|SKAP2 0.008230                0              1
#2 ENSG00000039319|ZFYVE16 0.121040                0              1
#3   ENSG00000087884|AAMDC 0.135390                1              0
#4  ENSG00000027869|SH2D2A 0.002489                1              1
#5   ENSG00000124608|AARS2 0.325000                0              0

或使用data.table

library(data.table)
setDT(g0)[, g_eGenes_nsPre := +(gene_id_name %in% nsPre$gene_id_name)]

样本数据

nsPre <- read.table(text =
"gene_id_name
ENSG00000005020|SKAP2
ENSG00000017260|ATP2C1
ENSG00000027869|SH2D2A
ENSG00000039319|ZFYVE16", header = T)
g0 <- read.table(text =
"gene_id_name            pLI       g_eGenes_general
ENSG00000005020|SKAP2   0.00823   0
ENSG00000039319|ZFYVE16 0.12104   0
ENSG00000087884|AAMDC   0.13539   1
ENSG00000027869|SH2D2A  0.002489  1
ENSG00000124608|AARS2   0.32500   0", header = T)

这是一个使用data.table的简单oneliner:首先使g_eGenes_nsPre列为全零,然后将gene_id_names在其他数据帧中的行的值更改为1。

library(data.table)
setDT(g0); setDT(nsPre)
g0[,g_eGenes_nsPre:=0][gene_id_name%in%nsPre$gene_id_name,g_eGenes_nsPre:=1]

相关内容

最新更新