R: losing column names when adding rows to an empty data frame

前端 未结 8 1162
旧时难觅i
旧时难觅i 2020-12-05 01:36

I am just starting with R and encountered a strange behaviour: when inserting the first row in an empty data frame, the original column names get lost.

example:

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 01:54

    FWIW, an alternative design might have your functions building vectors for the two columns, instead of rbinding to a data frame:

    ones <- c()
    twos <- c()
    

    Modify the vectors in your functions:

    ones <- append(ones, 5)
    twos <- append(twos, 6)
    

    Repeat as needed, then create your data.frame in one go:

    a <- data.frame(one=ones, two=twos)
    

提交回复
热议问题