Creating an R dataframe row-by-row

后端 未结 8 794
天涯浪人
天涯浪人 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:12

    Dirk Eddelbuettel's answer is the best; here I just note that you can get away with not pre-specifying the dataframe dimensions or data types, which is sometimes useful if you have multiple data types and lots of columns:

    row1<-list("a",1,FALSE) #use 'list', not 'c' or 'cbind'!
    row2<-list("b",2,TRUE)  
    
    df<-data.frame(row1,stringsAsFactors = F) #first row
    df<-rbind(df,row2) #now this works as you'd expect.
    

提交回复
热议问题