【问题标题】:Error in colnamesInt(i, unname(on), check_dups = FALSE) : argument specifying columns specify non existing column(s): cols[1]='ID_REF'colnamesInt(i, unname(on), check_dups = FALSE) 中的错误:指定列的参数指定不存在的列:cols[1]='ID_REF'
【发布时间】:2021-10-08 09:20:33
【问题描述】:

我有两个文件,datsamp.matrix是它的数据矩阵)和它对应的注解文件ann。我删除了samp.matrix 中的异常值,现在想通过删除相应的异常值来更新ann(如ann.filtered)。如何更新ann

samp.matrix <- data.matrix(dat[, (3:ncol(dat))])

# Remove Outlier(s) 
samp.matrix <- samp.matrix[, -(grep(names(outlier), colnames(samp.matrix)))]

# Eliminating probes with rowMeans less than 0 on a log2 scale
dat.fil <- subset(samp.matrix, log2(rowMeans(samp.matrix)) > 0)
removed <- nrow(samp.matrix) - nrow(dat.fil)

# Eliminate probes with rowMeans less than 3 on a log2 scale
dat.filtered <- subset(dat.fil, rowMeans(dat.fil) > 3)
dat.filtered <- as.data.frame(dat.filtered)
removed  <- nrow(dat.fil) - nrow(dat.filtered)


# Update annotation file
ID_REF <- rownames(ann)
ann <- cbind(ID_REF, ann)
rownames(ann) <- NULL
ann <- ann %>%  as.data.table()
dat.filtered <- dat.filtered %>%  as.data.table() 
ann.filtered <- ann[dat.filtered, on=.("ID_REF")] %>% 

select(ID_REF, Gene.title, Gene.symbol)

colnamesInt(i, unname(on), check_dups = FALSE) 中的错误:参数 指定列指定不存在的列:cols[1]='ID_REF'

dat

ID_REF IDENTIFIER GSM97800 GSM97804
1007_s_at MIR4640 4701.5 4735.0
1053_at RFC2 282.7 347.9
117_at HSPA6 769.6 287.9
121_at PAX8 1616.3 1527.2

dat.filtered

ID_REF IDENTIFIER GSM97800 GSM97804
1007_s_at MIR4640 4701.5 4735.0
1053_at RFC2 282.7 347.9

ann

ID_REF Gene.title Gene.symbol Gene.ID
1007_s_at microRNA 4640//discoidin domain receptor MIR4640//DDR1 100616
1053_at replication factor C subunit 2 RFC2 5982
117_at heat shock protein family A (Hsp70) HSPA6 3310
121_at paired box 8 PAX8 7849

预期输出 ann.filtered

ID_REF Gene.title Gene.symbol Gene.ID
1007_s_at microRNA 4640//discoidin domain receptor MIR4640//DDR1 100616
1053_at replication factor C subunit 2 RFC2 5982

【问题讨论】:

    标签: r dataframe filter statistics


    【解决方案1】:

    最简单的方法是加入这个表 ...与data.table

    library(data.table)
    library(dplyr)
    ann <- ann %>%  as.data.table() ##convert to data.table
    dat.filtered <- dat.filtered %>%  as.data.table() ##convert to data.table
    
    ann[dat.filtered] %>% ##join
      select(ID_REF, Gene.title, Gene.symbol, Gene.ID) ##remove cols except selected
    

    【讨论】:

    • 我尝试了您的代码,但改用了ann[dat.filtered] %&gt;% select(c(ID_REF, colnames(ann)))。我收到一个错误Error in [.data.table(ann, dat.filtered) : When i is a data.table (or character vector), the columns to join by must be specified using 'on=' argument (see ?data.table), by keying x (i.e. sorted, and, marked as sorted, see ?setkey), or by sharing column names between x and i (i.e., a natural join). Keyed joins might have further speed benefits on very large data due to x being sorted in RAM.
    • 假设您忘记了ann 中的第一个 col (ID_REF)。具有正确名称ann[dat.filtered, on = 'ID_REF']
    • 即使我添加了 colname ID_REF,我仍然收到 colname 不存在的错误。我已经更新了我上面的问题。
    【解决方案2】:

    由于您在两个数据框中都有一些共同的列,您可以进行左连接,然后只保留 ann 的列

    library(dplyr)
    
    #rename the first column to prevent having a blank column name in ann 
    colnames(ann) <- c("ID_REF","Gene.title","Gene.symbol","Gene.ID") 
    
    ### join both dataframes keeping only rows rows avaiable from dat.filtered
    ### And keep only the original columns from ann
    ann.filetered <- dat.filtered %>%
      left_join(ann, by=c("ID_REF","Gene.symbol","Gene.ID")) %>%
      select(ID_REF,Gene.title,Gene.symbol,Gene.ID)
    

    【讨论】:

      猜你喜欢
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 2019-10-21
      • 2018-12-11
      • 2016-12-12
      • 2022-06-22
      相关资源
      最近更新 更多