Search code examples
rlapplywritetofile

substitute a string space with newline in "r"


I'm using R. I have an excel file which each rows of it consists of two or three lines description. I wish to write each rows in a separate text file. So, suppose I have a excel file with 100 rows, I will need 100 separate text files.

Is there any idea on how to do it in an efficient way?


Solution

  • Save the file/ sheet as a csv and use read.csv to read in the csv as an R data frame.

    You can then use mapply with the data frame variable (say it's called x, with a column $desc for the description). Make a character vector with one entry for each row of your data frame called fnms. Use `fnms <- paste0("text", 1:nrow(x), ".txt") or similar.

    Put:

    mapply(write, x$desc, fnms)