0

I have a data.frame which is initially empty and then shall be filled:

df <- data.frame(datetime=as.POSIXct(character()), t=factor(), val=numeric()) 

Until now I could not found a solution to append rows continously to the end.

I am looking for a solution similar to this, but working of course:

df[nrow(df) + 1,] = c(as.POSIXct('2002-02-12 22:00:00',format='%Y-%m-%d %H:%M:%S', tz='UTC'),"test", 34.5)

How can this be achieved?

Thanks :-)

Sotos
  • 51,121
  • 6
  • 32
  • 66
Roland
  • 131
  • 7
  • Does this answer your question? [Add row to dataframe](https://stackoverflow.com/questions/28467068/add-row-to-dataframe) – ulfelder Jan 14 '20 at 10:41

1 Answers1

2

Maybe you can use rbind() + cbind() like below

df <- rbind(df, setNames(data.frame(as.POSIXct('2002-02-12 22:00:00',format='%Y-%m-%d %H:%M:%S', tz='UTC'),"test", 34.5),names(df)))

such that you will get

> df
             datetime    t  val
1 2002-02-12 22:00:00 test 34.5
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • 2
    There is also `setNames()` which can be more readable than ````colnames<-```` – Sotos Jan 14 '20 at 10:37
  • Thx, I am just wondering, that the datetime shows as integer and not as datetime string. What is the reason for this and how can it be changed? – Roland Jan 14 '20 at 10:50
  • 1
    @Roland I guess it is because of `cbind`... you can see my update, where `data.frame()` replaced `cbind()` – ThomasIsCoding Jan 14 '20 at 10:57