I have the problem that I have to split a document into different csv files having the the title of the column that I have used to split the original file.
As several users will have to use this script, I have designed it in a way that every user just has to enter once his working directory and the the other folders mentioned within the script are created based on the users' working directory. This looks as follows:
# Determination of user specific location
base_direction <- "C:/users/USER1/Desktop"
# Create "FolderA"
folder_foldera <- "FolderA"
path_foldera <- file.path(base_direction, folder_foldera)
dir.create(path_foldera)
# Create "FolderA/Data"
folder_foldera_data <- "Data"
path_foldera_data <- file.path(base_direction, folder_foldera, folder_foldera_data)
dir.create(path_foldera_data)
The creation of those folders works perfectly fine. Then I have to save single documents within the folder "Data", which I have created in "FolderA". Herefore, I use this code:
# Filtering and separating into different files
listed <- split(table.t, table.t$sss)
sapply(names(listed),
function (x) write.table(listed[[x]], file=paste0(path_foldera_data, x, ".csv")))
The splitting of the files works perfectly fine, but with the correct saving I have some problems. Right now, the single documents are saved in "FolderA" besides the folder "Data" like this:
However, I would like to have the data saved within the folder "Data" like this:
I think that there is an error within the part write.table(listed[[x]], file=paste0(path_foldera_data, x, ".csv"))
, but as I am not so used to R yet, I have no idea where to look for it and I would be very grateful, if you could help me solving this problem.
Thank you very much!!!
Try adding "/" in your write.table
command
sapply(names(listed), function (x)
write.table(listed[[x]], file=paste0(path_foldera_data, "/", x, ".csv")))