Reputation: 11
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")
Upvotes: 1
Views: 64
Reputation: 1495
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])))
Upvotes: 0
Reputation: 101099
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"))
}
Upvotes: 3