[R] Add columns to data frame automatically

jim holtman jholtman at gmail.com
Fri Feb 13 20:54:13 CET 2009


Something like this might work where you are using 'lapply' to create
a list of values from the equivalent of the 'for' loop that you had.
You can then 'cbind' them to create a return matrix, or you can
convert it to a dataframe:

prep <- function()
{
# Clase[1]/Categoria[2]/Phi[3]/Rf[4]
  peso <- c(.0,.03,.3,.6)
# Extension del calculo
  result <- lapply(1:100, function(){

    # Calculos de todas las curvas
      # Variables (Valor Base)
      abase <- AlicuotaBruta
      clase <- llmcc$Clase
      as.numeric(peso)

      retValue <- (abase/llmcc$Clase)*peso[1]
      peso[1] <- peso[1]+(.01)
      cat("Modelos de Alicuotas:", i, peso[1], "\n")
      retValue
})
return (do.call(cbind, result))
}

On Fri, Feb 13, 2009 at 2:23 PM, Jesús Guillermo Andrade
<jgandradev at mac.com> wrote:
> Hello fellows: I've een trying to set up a function that performs 100
> loops producing the coresponding 100 series. I want to save all those
> datasets in a dataframe, so I wrote this...
>
> prep <- function()
>
> # Clase[1]/Categoria[2]/Phi[3]/Rf[4]
>   peso <- c(.0,.03,.3,.6)
> # Extension del calculo
>   for (i in 1:100)
> {
> # Calculos de todas las curvas
>   # Variables (Valor Base)
>   abase <- AlicuotaBruta
>   clase <- llmcc$Clase
>   as.numeric(peso)
>   df.clases <- data.frame()
>   df.clases[,i] <- (abase/llmcc$Clase)*peso[1]
>   peso[1] <- peso[1]+(.01)
>   cat("Modelos de Alicuotas:", i, peso[1], "\n")
> }
> return (df.clases)
>
> Problem is: the function does not work, and I just cant seem to
> understand how to save the results for every iteration where the value
> of i  and peso[1] changes.
> Any ideas?
>
>
>
> Hágale un favor a alguien y le tendrá que hacer muchos mas. Ley de
> Pinto.
> ------------------------------
> Jesús Guillermo Andrade (Abg.)
> Gerente de Litigios y Corporativo. EDM. AC. API.
> Andrade & Moreno S.C. (http://amlegal.wordpress.com/)
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list