Function to export workspace objects(data frames) to csv files
-
11-01-2021 - |
سؤال
I want to batch export data frames in my workspace having the string "obj" in their names to a csv file with the same name
I thought of the following code, which unfortunately does not work.
for (i in ls(pattern="obj")){
write.csv2(i, paste(i,".csv", sep=""))
}
Any ideas? Thank you in advance
المحلول
write.csv2
takes the object rather than the name of the object as its first argument. Use get
to remedy this.
write.csv2(get(i), paste(i,".csv", sep=""))
نصائح أخرى
You can do it with eval
:
eval(call("write.csv2", as.name(i), paste(i, ".csv", sep="")))
لا تنتمي إلى StackOverflow