【问题标题】:Using Loop inside a function with If statement in R在 R 中使用带有 If 语句的函数中使用循环
【发布时间】:2021-06-24 08:07:16
【问题描述】:

我最近正在学习 R 中的函数。我成功地使这个功能工作了

check_spot = function(one_spot, cluster_id, marker_gene){
one_spot = sample(glio_spatial@assays$SCT@misc$vst.out$cells_step1, 1)
cluster_id  = sample(as.data.frame(Idents(glioblastoma))[,1], 1)
marker = FindMarkers(glioblastoma, ident.1 = cluster_id)
marker = cbind(gene = rownames(marker), marker)
rownames(marker) = 1:nrow(marker)
marker_gene = sample(marker[,1], 5)
a = as.data.frame(glio_spatial@assays$Spatial@counts)
b = a[one_spot]
b = rename(b,c("count_spot" = all_of(one_spot)))
b = subset(b, b[,1] != 0, )
b = cbind(gene = rownames(b), b)
rownames(b) = 1:nrow(b)
intersection = inner_join(b, marker)
if (marker_gene[1] %in% intersection[1,]) sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)
else if
     (marker_gene[2] %in% intersection[1,]) sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)
else if
     (marker_gene[3] %in% intersection[1,]) sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)
else if
     (marker_gene[4] %in% intersection[1,]) sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)
else if
     (marker_gene[5] %in% intersection[1,]) sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)
else
    sprintf("The gene %s is a marker gene for the cluster %s, but not expressed in the spot %s", marker_gene, cluster_id, one_spot)
 }

它会打印这个: 加入, by = "gene"

'NKD1基因是簇8的标记基因,但在CTACCCTAAGGTCATA-1位点不表达'

'基因RPL8是簇8的标记基因,但在斑点CTACCCTAAGGTCATA-1中不表达'

'基因TSPAN13是簇8的标记基因,但在斑点CTACCCTAAGGTCATA-1中不表达'

'基因HSBP1是簇8的标记基因,但在斑点CTACCCTAAGGTCATA-1中不表达'

'BHLHE41基因是簇8的标记基因,但在CTACCCTAAGGTCATA-1位点中不表达'。

然后,我尝试使用 for 语句为该函数创建一个更简单的函数以获取更通用的样本数量,我试试这个

check_spot = function(one_spot, cluster_id, marker_gene){
one_spot = sample(glio_spatial@assays$SCT@misc$vst.out$cells_step1, 1)
cluster_id  = sample(as.data.frame(Idents(glioblastoma))[,1], 1)
marker = FindMarkers(glioblastoma, ident.1 = cluster_id)
marker = cbind(gene = rownames(marker), marker)
rownames(marker) = 1:nrow(marker)
marker_gene = sample(marker[,1], length(marker[,1]))
a = as.data.frame(glio_spatial@assays$Spatial@counts)
b = a[one_spot]
b = rename(b,c("count_spot" = all_of(one_spot)))
b = subset(b, b[,1] != 0, )
b = cbind(gene = rownames(b), b)
rownames(b) = 1:nrow(b)
intersection = inner_join(b, marker)
for (i in 1:length(marker_gene)){
    if (marker_gene[i] %in% intersection[1,]) {sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)}
    else {sprintf("The gene %s is a marker gene for the cluster %s, but not expressed in the spot %s", marker_gene, cluster_id, one_spot)}
}}

当我调用 check_spot() 时,它不会显示错误,也不会显示输出。可以请任何人帮忙,这样我就可以得到第一个函数所示的类似结果(但一般来说,更多的标记)?即使我只为标记 5 取样,它仍然不起作用。非常感谢。

【问题讨论】:

  • 你需要一个语言标签
  • 已经编辑了。
  • 你的函数没有返回值。通常在 R 中,function 范围内的最后一行代码被视为返回值。但一个好的做法是使用return()。话虽如此,请考虑包含一个数据集样本,以帮助您发现任何问题
  • @ChrissPaul 我想我已经用 sprintf() 为函数创建了一个返回值,因为 sprintf 是执行的最后一行?
  • 一个函数应该在参数中包含所有输入,但是在你的函数中,你没有正确声明参数,实际上所有输入参数都将被后续代码覆盖。正确声明输入 --

标签: r function loops if-statement seurat


【解决方案1】:

我通过以下方式解决问题:

for (i in 1:length(marker_gene)){
    if (marker_gene[i] %in% intersection[1,]) return(sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot))
    else return(sprintf("The gene %s is a marker gene for the cluster %s, but not expressed in the spot %s", marker_gene, cluster_id, one_spot))
}

【讨论】:

    【解决方案2】:

    正确声明输入参数

    #Input should be something like this
    check_spot = function(glio_spatial,glioblastoma){
    

    【讨论】:

    • 我对你的意思感到困惑。我的函数参数是 function(one_spot, cluster_id, marker_gene) 并且我在函数体中将这些参数定义为随机的。因此,当我调用我的函数时,我只需要使用 check_spot(),并且在运行该函数之前,我已经运行了所有需要的数据集。能否详细解释一下?
    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    • 2022-01-04
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多