valie15
valie15

Reputation: 3

Creating an R loop for writing CSV files from tables

I have a number of tables in R studio, named "zero.cells", "one.cells" (etc) up to "fifteen.cells".

I would like to write all of these tables in csv using write.csv2, for example write.csv2(zero.cells, file = "zero.cells.csv")

Is there a loop I can use to get this done rather than having to write all of this out for every single file? Thanks!

Upvotes: 0

Views: 557

Answers (2)

Henry Navarro
Henry Navarro

Reputation: 953

I had a different and may be harder way than @TobiO.

I used a function from here: link

numbers2words <- function(x){
  ## Function by John Fox found here: 
  ## http://tolstoy.newcastle.edu.au/R/help/05/04/2715.html
  ## Tweaks by AJH to add commas and "and"
  helper <- function(x){

    digits <- rev(strsplit(as.character(x), "")[[1]])
    nDigits <- length(digits)
    if (nDigits == 1) as.vector(ones[digits])
    else if (nDigits == 2)
      if (x <= 19) as.vector(teens[digits[1]])
    else trim(paste(tens[digits[2]],
                    Recall(as.numeric(digits[1]))))
    else if (nDigits == 3) trim(paste(ones[digits[3]], "hundred and", 
                                      Recall(makeNumber(digits[2:1]))))
    else {
      nSuffix <- ((nDigits + 2) %/% 3) - 1
      if (nSuffix > length(suffixes)) stop(paste(x, "is too large!"))
      trim(paste(Recall(makeNumber(digits[
        nDigits:(3*nSuffix + 1)])),
        suffixes[nSuffix],"," ,
        Recall(makeNumber(digits[(3*nSuffix):1]))))
    }
  }
  trim <- function(text){
    #Tidy leading/trailing whitespace, space before comma
    text=gsub("^\ ", "", gsub("\ *$", "", gsub("\ ,",",",text)))
    #Clear any trailing " and"
    text=gsub(" and$","",text)
    #Clear any trailing comma
    gsub("\ *,$","",text)
  }  
  makeNumber <- function(...) as.numeric(paste(..., collapse=""))     
  #Disable scientific notation
  opts <- options(scipen=100) 
  on.exit(options(opts)) 
  ones <- c("", "one", "two", "three", "four", "five", "six", "seven",
            "eight", "nine") 
  names(ones) <- 0:9 
  teens <- c("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
             "sixteen", " seventeen", "eighteen", "nineteen")
  names(teens) <- 0:9 
  tens <- c("twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty",
            "ninety") 
  names(tens) <- 2:9 
  x <- round(x)
  suffixes <- c("thousand", "million", "billion", "trillion")     
  if (length(x) > 1) return(trim(sapply(x, helper)))
  helper(x)
}


list_names<-gsub(" ",".",paste0(numbers2words(1:50),".cells"))

my_final_df<-data.frame()
for (name in list_names){
  my_final_df<-rbind(my_final_df,get(name))

}

write.csv2(my_final_df,"yourfile.csv")

Upvotes: 0

TobiO
TobiO

Reputation: 1381

I guess the names of the objects in Rstudio are all having .cells in their names? And other objects with that name part do not exist? Then the following would work:

dataframe_names=ls(pattern="\\.cells") #find all the objects in your environment with ".cells" in their name. 
#I suggest checking this by printing the content of dataframe_names to the console:
dataframe_names

# if these are all correct run the following

for(dfn in dataframe_names){
    write.csv2(get(dfn),file=paste(dfn,".csv")
}

Upvotes: 2

Related Questions