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

前端 未结 8 1168
旧时难觅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:52

    I use the following solution to add a row to an empty data frame:

    d_dataset <- 
      data.frame(
        variable = character(),
        before = numeric(),
        after = numeric(),
        stringsAsFactors = FALSE)
    
    d_dataset <- 
      rbind(
        d_dataset,
          data.frame(
            variable = "test",
            before = 9,
            after = 12,
            stringsAsFactors = FALSE))  
    
    print(d_dataset)
    
    variable before after  
    1     test      9    12
    

    HTH.

    Kind regards

    Georg

提交回复
热议问题