Question: Error: unexpected '}' in " }" in R codes of Seurat
0
gravatar for vHelmholtz
12 months ago by
vHelmholtz20
United States
vHelmholtz20 wrote:

Hello,

I am practicing R codes in the Seurat tutorials. When I type

for (i in 0:12) {
  g[i].marker <- FindConservedMarkers(immune.combined, ident.1 = i, 
  grouping.var = "stim", verbose = FALSE)
   }

It gives me an error as below.

Error: unexpected symbol in: "for (i in 0:12) {   g[i].marker"
  } Error: unexpected '}' in "  }"

How can I fix this error? Please help!

seurat R • 428 views
ADD COMMENTlink modified 12 months ago by noorpratap.singh300 • written 12 months ago by vHelmholtz20
2
gravatar for vHelmholtz
12 months ago by
vHelmholtz20
United States
vHelmholtz20 wrote:

Hi Fatima,

Thank you for your advice. It gives me 'g_marker not found'. Could you give me any suggestion about this issue?

Thanks in advance!

> for (i in 0:12) {
    g_marker[i] <- FindConservedMarkers(immune.combined, ident.1 = i, 
    grouping.var = "stim", verbose = FALSE)
    }

Error: object 'g_marker' not found
ADD COMMENTlink modified 11 months ago • written 12 months ago by vHelmholtz20

what if you add g_marker <- 0 before the for loop to initialize the array?

https://www.datacamp.com/community/tutorials/tutorial-on-loops-in-r

could you try this command to see what is the output? I assume it's of type string.

a <- FindConservedMarkers(immune.combined, ident.1 = 0, 
    grouping.var = "stim", verbose = FALSE)
class(a)

Also you can try with a simpler code to see if your loop and your array work as expected, for example these scripts works for me

Numeric array:

g_marker <- 0
for (i in 0:4) { g_marker[i] <- i }
g_marker
> g_marker
[1] 1 2 3 4

String array:

# initialize the array with ""
g_marker <- ""
for (i in 0:4) { g_marker[i] <- sprintf("Gene %d",i) }
g_marker
> g_marker
[1] "Gene 1" "Gene 2" "Gene 3" "Gene 4"
ADD REPLYlink modified 11 months ago • written 11 months ago by Fatima890

Hi Fatima,

Thank you for your kind advice. I tried the simpler code and found that it works as expected. However, when I type add g_marker <- 0 before the for loop,

g_marker <- 0
for (i in 0:12) {
  g_marker[i] <- FindConservedMarkers(immune.combined, ident.1 = i, 
  grouping.var = "stim", verbose = FALSE)
  }

it gave me errors as below.

There were 12 warnings (use warnings() to see them)    
> warnings()
Warning messages:
1: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
2: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
3: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
4: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
5: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
6: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
7: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
8: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
9: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
10: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
11: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
12: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
ADD REPLYlink modified 11 months ago • written 11 months ago by vHelmholtz20

Could you just run

FindConservedMarkers(immune.combined, ident.1 = 0, 
    grouping.var = "stim", verbose = FALSE)

And see what the output is? It might be a complicated output and we need to see the format before deciding on how to save it in an array.

based on the link below the output is a data.frame. https://www.rdocumentation.org/packages/Seurat/versions/3.1.2/topics/FindConservedMarkers

For a simple string output (e.g Gene1) you could add g_marker <- "" before the loop instead of 0, if you post a sample output I might be able to help on how to save it in a suitable form. For example if the output is a big data.frame then you can write the result of each iteration in a new csv file or you can use a script like this:

for (i in 0:12) {
g <- paste("g_marker_", i, sep = "")
assign(g, FindConservedMarkers(immune.combined, ident.1 = i, 
        grouping.var = "stim", verbose = FALSE))
#print(g_marker_i)
}

Simpler script:

g <- paste("g_marker", 1, sep = "")
assign(g, 10)
> g_marker1
10

Another simpler but irrelevant script:

g <- paste("g_marker", 1, sep = "")
# create a sample dataframe
set.seed(1)
mydf <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100))
assign(g, mydf)
print(g_marker1)

https://thomasleeper.com/Rcourse/Tutorials/savingdata.html

https://stackoverflow.com/questions/16566799/change-variable-name-in-for-loop-using-r

ADD REPLYlink modified 11 months ago • written 11 months ago by Fatima890

Hi Fatima,

Thank you so much for your comments. I add g_marker <- "" before the loop and also got an error as below, which is similar to the result of g_marker <- 0.

 > g_marker <- ""
 > for (i in 0:12) {
+   g_marker[i] <- FindConservedMarkers(immune.combined, ident.1 = i, 
    grouping.var = "stim", verbose = FALSE)
+   }
There were 12 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
2: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
3: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
4: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
5: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
6: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
7: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
8: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
9: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
10: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
11: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
12: In g_marker[i] <- FindConservedMarkers(immune.combined,  ... :
  number of items to replace is not a multiple of replacement length
ADD REPLYlink modified 11 months ago • written 11 months ago by vHelmholtz20

Since the output is a data.frame you can do the following as mentioned in my previous comment.

for (i in 0:12) {
g <- paste("g_marker_", i, sep = "")
assign(g, FindConservedMarkers(immune.combined, ident.1 = i, 
        grouping.var = "stim", verbose = FALSE))
print(g_marker_i)
}

You can read more about paste and assign here: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/paste https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/assign

You can also use this script to save each result in a file:

for (i in 0:12) {
filename <- paste("g_marker", i, ".csv", sep = "")
result <- FindConservedMarkers(immune.combined, ident.1 = i, 
        grouping.var = "stim", verbose = FALSE)
write.csv(result, file=filename)
}
# this command gives you the current directory
getwd()

Then you can find the following files in your current directory:

g_marker1.csv, g_marker2.csv,.....

Another simple approach can be just manually assigning values 1 to 12 to ident.1:

    g_marker_1 <- FindConservedMarkers(immune.combined, ident.1 = 1, 
            grouping.var = "stim", verbose = FALSE)
   .....
    g_marker_12 <- FindConservedMarkers(immune.combined, ident.1 = 12, 
            grouping.var = "stim", verbose = FALSE)
ADD REPLYlink modified 11 months ago • written 11 months ago by Fatima890

Hi Fatima,

Thank you for your advice. Your code works very well.

for (i in 0:12) {
  g <- paste("g_marker_", i, sep = "")
  assign(g, FindConservedMarkers(immune.combined, ident.1 = i, 
                                  grouping.var = "stim", verbose = FALSE))
  #print(g_marker_i)
}
ADD REPLYlink written 11 months ago by vHelmholtz20

do the following:

g_marker <-  list()
for (i in 0:12) {
   g_marker[[i]] <- FindConservedMarkers(immune.combined, ident.1 = i, 
   grouping.var = "stim", verbose = FALSE)
}
ADD REPLYlink modified 11 months ago • written 11 months ago by noorpratap.singh300
1
gravatar for Fatima
12 months ago by
Fatima890
United states
Fatima890 wrote:

I think the problem is with g[i].marker maybe using g_marker[i] would solve the problem.

**update: since the output of FindConservedMarkers is a data.frame you can use a script similar to these ones:

for (i in 0:12) {
g <- paste("g_marker_", i, sep = "")
assign(g, FindConservedMarkers(immune.combined, ident.1 = i, 
        grouping.var = "stim", verbose = FALSE))
print(g_marker_i)
}

for (i in 0:12) {
filename <- paste("g_marker", i, ".csv", sep = "")
result <- FindConservedMarkers(immune.combined, ident.1 = i, 
        grouping.var = "stim", verbose = FALSE)
write.csv(result, file=filename)
}

https://www.rdocumentation.org/packages/Seurat/versions/3.1.2/topics/FindConservedMarkers https://www.tutorialspoint.com/r/r_data_frames.htm

ADD COMMENTlink modified 11 months ago • written 12 months ago by Fatima890
1
gravatar for noorpratap.singh
12 months ago by
University of Maryland
noorpratap.singh300 wrote:

The error is happening is because of g[i].marker. Are you sure there exists a variable/list as g.

Assuming it exists, g[i] would point to an appropriate element (only for a vector, if g is a list then g[[i]] has to be used). The "." following creates the error. Unlike other languages, R is based on Lisp where "." is used as a part of naming convention and is used in place of "_' for naming variables compared to other languages such as python, C. Further, compared to python where dot operator is used to access the object attributes and functions, it does not have special properties for most of the cases (barring formulas) in R.

ADD COMMENTlink written 12 months ago by noorpratap.singh300
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1628 users visited in the last hour
_