Generating variable names for dataframes based on the loop number in a loop in R

前端 未结 2 1138
执念已碎
执念已碎 2021-01-29 02:32

I am working on developing and optimizing a linear model using the lm() function and subsequently the step() function for optimization. I have added a variable to my dataframe b

相关标签:
2条回答
  • 2021-01-29 03:00

    Don't create objects with numbers in their names, and then try and access them in a loop later, using get and paste and assign. The right way to do this is to store your elements in an R list object.

    0 讨论(0)
  • 2021-01-29 03:02

    For your first question:

    You can create the strings as before, using

    df.names <- paste(("Run",1:10,sep="")
    

    Then, create your for loop and do the following to give the data frames the names you want:

    for (i in 1:10){
       d.frame <- # create your data frame here
       assign(df.name[i], d.frame)
    }
    

    Now you will end up with ten data frames with ten different names.

    For your second question about the coefficients:

    As far as I can tell, these don't naturally fit into your data frame structure. You should consider using lists, as they allow different classes - in other words, for each run, create a list containing a data frame and a numeric vector with your coefficients.

    0 讨论(0)
提交回复
热议问题