I have some bloated code in R which I'm trying to streamline. I'm trying to read spreadsheets into a dataframe and then transpose each one.
I have a list as follows
var <- c("amp_genes.annotated.BLCA.txt","amp_genes.annotated.BRCA.txt")
for (i in var) {
var[i] <- readWorksheet(wk, sheet="var[i]", header=T)
var[i] <- as.data.frame(var[i])
var[i] <- t(var1[i][3:ncol(var1[i]),])
}
The sheet = line has to have double quotes around the string variable.
This just tells me I have an unexpected }
"var[i]"- it will literally pass the textvar[i]exactly as shown, with no interpretation. You wantsheet=iasiwill contain the text of thevarvector, as infor (i in c("a","b") ) print(i)varvector, are those sheet names? Or are they names you wish to give to your output (delimited) files? Also, note that you are overwriting yourvariterator at the first line inside your loop, which in not a good idea!varwill take valueamp_genes_...at its first iteration, as a character variable. Then you put it in something else. An iterator should be left alone for the whole duration of the loop!varis a vector, you cannot put adata.frameas first element but if you want you could (instead of your loop) do something likesapply(var, function(x){readWorksheet(wk, sheet=x, header=T)and you'll get a list of data.frame corresponding to your sheets