title: Using Loop inside a function with If statement in R
body: <p>I am learning about function in R recently. I successfully make this function work</p>
  <pre><code>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(&quot;count_spot&quot; = 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(&quot;The gene %s is a marker gene for the cluster %s and is expressed in the spot %s&quot;, marker_gene, cluster_id, one_spot)
  else if
       (marker_gene[2] %in% intersection[1,]) sprintf(&quot;The gene %s is a marker gene for the cluster %s and is expressed in the spot %s&quot;, marker_gene, cluster_id, one_spot)
  else if
       (marker_gene[3] %in% intersection[1,]) sprintf(&quot;The gene %s is a marker gene for the cluster %s and is expressed in the spot %s&quot;, marker_gene, cluster_id, one_spot)
  else if
       (marker_gene[4] %in% intersection[1,]) sprintf(&quot;The gene %s is a marker gene for the cluster %s and is expressed in the spot %s&quot;, marker_gene, cluster_id, one_spot)
  else if
       (marker_gene[5] %in% intersection[1,]) sprintf(&quot;The gene %s is a marker gene for the cluster %s and is expressed in the spot %s&quot;, marker_gene, cluster_id, one_spot)
  else
      sprintf(&quot;The gene %s is a marker gene for the cluster %s, but not expressed in the spot %s&quot;, marker_gene, cluster_id, one_spot)
   }
  </code></pre>
  <p>It will print this:
  Joining, by = &quot;gene&quot;</p>
  <p>'The gene NKD1 is a marker gene for the cluster 8, but not expressed in the spot CTACCCTAAGGTCATA-1'</p>
  <p>'The gene RPL8 is a marker gene for the cluster 8, but not expressed in the spot CTACCCTAAGGTCATA-1'</p>
  <p>'The gene TSPAN13 is a marker gene for the cluster 8, but not expressed in the spot CTACCCTAAGGTCATA-1'</p>
  <p>'The gene HSBP1 is a marker gene for the cluster 8, but not expressed in the spot CTACCCTAAGGTCATA-1'</p>
  <p>'The gene BHLHE41 is a marker gene for the cluster 8, but not expressed in the spot CTACCCTAAGGTCATA-1'.</p>
  <p>Then, I have tried to make a simpler function for this function using for statement to get more generalise number of samples, I try this</p>
  <pre><code>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(&quot;count_spot&quot; = 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(&quot;The gene %s is a marker gene for the cluster %s and is expressed in the spot %s&quot;, marker_gene, cluster_id, one_spot)}
      else {sprintf(&quot;The gene %s is a marker gene for the cluster %s, but not expressed in the spot %s&quot;, marker_gene, cluster_id, one_spot)}
  }}
  </code></pre>
  <p>When I call check_spot(), it does not show errors and does not show the output as well.  Could please anyone help so I can get the similar result (but ore general, more number of markers) as shown by the first function? Even if I take the sample for marker only 5, it still does not work. Thank you very much.</p>