修正R中的函数

时间:2015-12-11 22:27:31

标签: r function

我正在尝试编写一个清理大型数据帧的函数。以下功能无效。任何有关如何使这项工作的想法非常感谢!

function(x){
    x_4 <- x[x$gene_affected %in% c("ENSG00000171862","ENSG00000121879", "ENSG00000051382", "ENSG00000198793", "ENSG00000142208", "ENSG00000105221", "ENSG00000117020", "ENSG00000145675", "ENSG00000165699", "ENSG00000103197", "ENSG00000104884"), ]
    x_5 <- x_4[x_4$consequence_type %in% c("missense_variant", "inframe_deletion", "frameshift_variant", "stop_gained", "inframe_insertion", "stop_lost", "stop_retained_variant", "splice_acceptor_variant", "splice_donor_variant", "splice_region_variant", "synonymous_variant"), ]  
    x_6 <- x_5[myvars]
    x_7<- x_6[!duplicated(x_6), ]
}

2 个答案:

答案 0 :(得分:1)

如果没有明确解释您的预期结果和实际结果,很难回答您的问题,但我的第一个猜测是您需要返回函数的值,像这样:

f = function(x){
    x_4 <- x[x$gene_affected %in% c("ENSG00000171862","ENSG00000121879", "ENSG00000051382", "ENSG00000198793", "ENSG00000142208", "ENSG00000105221", "ENSG00000117020", "ENSG00000145675", "ENSG00000165699", "ENSG00000103197", "ENSG00000104884"), ]
    x_5 <- x_4[x_4$consequence_type %in% c("missense_variant", "inframe_deletion", "frameshift_variant", "stop_gained", "inframe_insertion", "stop_lost", "stop_retained_variant", "splice_acceptor_variant", "splice_donor_variant", "splice_region_variant", "synonymous_variant"), ]  
    x_6 <- x_5[myvars]
    x_7<- x_6[!duplicated(x_6), ]
    # if you don't have this return phrase below, the function will not produce anything
    return(list(x=x, x_4=x_4, x_5=x_5, x_6=x_6, x_7=x_7))
}

如果您分配结果:

result <- f(x)

然后您可以使用

访问结果
result[['x_7']]

请考虑阅读这篇关于如何在R中编写函数的简短文章:

http://www.statmethods.net/management/userfunctions.html

答案 1 :(得分:1)

如果没有数据,很难生成一组有效的代码。但是代码末尾的print(x_7)将使函数打印出您想要的内容。

yourFunction <- function(x){
                x_4 <- x[x$gene_affected %in% c("ENSG00000171862","ENSG00000121879", 
                        "ENSG00000051382", "ENSG00000198793", "ENSG00000142208", 
                        "ENSG00000105221", "ENSG00000117020", "ENSG00000145675", 
                        "ENSG00000165699", "ENSG00000103197", "ENSG00000104884"), ]
                x_5 <- x_4[x_4$consequence_type %in% c("missense_variant", "inframe_deletion", 
                        "frameshift_variant", "stop_gained", "inframe_insertion", 
                        "stop_lost", "stop_retained_variant", "splice_acceptor_variant", 
                        "splice_donor_variant", "splice_region_variant", "synonymous_variant"), ]  
                x_6 <- x_5[myvars]
                x_7<- x_6[!duplicated(x_6), ]
                print(x_7)
             }

resultDF <- yourFunction(yourDataFrame)