Creating an R dataframe row-by-row

后端 未结 8 768
天涯浪人
天涯浪人 2020-11-29 15:52

I would like to construct a dataframe row-by-row in R. I\'ve done some searching, and all I came up with is the suggestion to create an empty list, keep a list index scalar,

相关标签:
8条回答
  • 2020-11-29 16:20

    I've found this way to create dataframe by raw without matrix.

    With automatic column name

    df<-data.frame(
            t(data.frame(c(1,"a",100),c(2,"b",200),c(3,"c",300)))
            ,row.names = NULL,stringsAsFactors = FALSE
        )
    

    With column name

    df<-setNames(
            data.frame(
                t(data.frame(c(1,"a",100),c(2,"b",200),c(3,"c",300)))
                ,row.names = NULL,stringsAsFactors = FALSE
            ), 
            c("col1","col2","col3")
        )
    
    0 讨论(0)
  • 2020-11-29 16:27

    You can grow them row by row by appending or using rbind().

    That does not mean you should. Dynamically growing structures is one of the least efficient ways to code in R.

    If you can, allocate your entire data.frame up front:

    N <- 1e4  # total number of rows to preallocate--possibly an overestimate
    
    DF <- data.frame(num=rep(NA, N), txt=rep("", N),  # as many cols as you need
                     stringsAsFactors=FALSE)          # you don't know levels yet
    

    and then during your operations insert row at a time

    DF[i, ] <- list(1.4, "foo")
    

    That should work for arbitrary data.frame and be much more efficient. If you overshot N you can always shrink empty rows out at the end.

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