1

I need to add some blank columns to multiple Excel files using R and then export them to my desktop.

I have managed to get the code working for one excel file but can't figures out how to loop it for files between 17 to 32.

r17 <- read_excel("r17.xlsx")
write.csv(r17, file = "C:/Users/Dr Wani/Dropbox/shazan/r17.csv")

2 Answers 2

3

What about

for (i in 17:32) {
  write.csv(read_excel(paste0("r", i, ".xlsx")), file = paste0("C:/Users/Dr Wani/Dropbox/shazan/r", i, ".csv"))
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could read all of your csv files into a list and apply some transformation function to the list elements (which are data frames). Then you write the adjusted files back to your computer using another lapply

files <- list.files(path = ".", pattern = "*.csv")

data <- lapply(files, read.csv, stringsAsFactors = FALSE)
names(data) <- basename(files)

# do some operation to your data using e.g. lapply

lapply(seq_len(length(data)), FUN = function(x) write.csv(data[[x]], file = names(data[x])))

Comments

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.