2

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 }

8
  • 6
    R is not a macro language, you can't do "var[i]" - it will literally pass the text var[i] exactly as shown, with no interpretation. You want sheet=i as i will contain the text of the var vector, as in for (i in c("a","b") ) print(i) Commented Apr 8, 2015 at 6:54
  • So in order to call the correct sheet I need to put double quotes around the text in the readworksheet line. How do I then append the double quotes to i? Commented Apr 8, 2015 at 7:09
  • 1
    what's in your var vector, are those sheet names? Or are they names you wish to give to your output (delimited) files? Also, note that you are overwriting your var iterator at the first line inside your loop, which in not a good idea! Commented Apr 8, 2015 at 7:37
  • 1
    You're still overwriting your iterator. var will take value amp_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! Commented Apr 8, 2015 at 7:48
  • 3
    var is a vector, you cannot put a data.frame as first element but if you want you could (instead of your loop) do something like sapply(var, function(x){readWorksheet(wk, sheet=x, header=T) and you'll get a list of data.frame corresponding to your sheets Commented Apr 8, 2015 at 7:48

1 Answer 1

2

Maybe try this; not sure it will work as I don't have your spreadsheets, but give it a try and let me know... And maybe if it doesn't work right out, it can hopefully unblock you wherever you're stuck.

library(XLConnect)
wk <- loadWorkbook("workbookname.xls")

sheetnames <- getSheets(object = wk)

content.tr <- list()

# To access sheets by their names
for (sheetname in sheetnames) {
  content <- readWorksheet(wk, sheet=sheetname, header=T)
  content.tr[[sheetname]] <- t(content[3:ncol(content),])
}

# To access sheets by their position
for (pos in c(1,2) {
  content <- readWorksheet(wk, sheet=i, header=T)
  content.tr[[sheetname[i]]] <- t(content[3:ncol(content),])
}

To access the dataframes:

names(content.tr)
spreadsheet1 <- content.tr[[1]]
spreadsheet2 <- content.tr[[2]]
Sign up to request clarification or add additional context in comments.

6 Comments

Ok with this is get Error in 3:ncol(content) : argument of length 0. Is this because sheetname when passed to sheet needs to be in double quotes
I corrected a few problems with the code, hopefully you have better luck with it now.
Changed a couple of typos above. Now its almost perfect. As an educational point how then do I refer to the dataframe throughout further code I want to put into the loop. Do I refer to it as content.tr[[sheetname[i]]] ? I will want to rename the first column for example as I go so should this then be content.tr[[sheetname]][,1] = c("chr")
Yes that would be perfectly acceptable! Although you could just do your transformations on content and then put the thing in the list as the last operation inside the loop.
...except for the fact that it doesnt relabel the column name, it puts 'chr' in every single row. How would I make it just change the column name?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.