[R] Problem with adding a row in a data table

Filippos Katsios katsiosf at gmail.com
Sun Sep 4 15:22:48 CEST 2016


Dear All,

I am relatively new to R and certainly new to the e-mailing list. I need
your help. I am working on a data frame, which looks like this:

Prod_name |  Date 1  |  Date 2 |  Date 3  |
------------------|-------------|------------|--------------|
Product 1    |     3      |      4     |     0       |
------------------|-------------|------------|--------------|
Product 2    |     5      |      3     |     3       |
------------------|-------------|------------|--------------|
Product 3    |     2      |      8     |     5       |

I am trying to add a new row with the following results:

Prod_name |  Date 1  |  Date 2 |  Date 3  |
------------------|-------------|------------|--------------|
Day_num    |     1      |      2     |      3      |
------------------|-------------|------------|--------------|
Product 1    |     3      |      4     |     0       |
------------------|-------------|------------|--------------|
Product 2    |     5      |      3     |     3       |
------------------|-------------|------------|--------------|
Product 3    |     2      |      8     |     5       |

Bellow you can find the things I tried and the results.
1)
r <- 1
newrow <- rep(1:7, 5, len=ncol(data_may)-1)
insertRow <- function(data_may, newrow, r) {
  data_may[seq(r+1,nrow(data_may)+1),] <- data_may[seq(r,nrow(data_may)),]
  data_may[r,] <- newrow
  data_may
}

It doesn't put the new row.
2)
data_may<-rbind(data_may,c("Day_num",newrow))

Error: cannot convert object to a data frame

3)
data_may[2093,]<-c("Day_num",rep(1:7, 5, len=ncol(data_may)-1))

It makes all the columns characters and when i try to change it it says
that you can change a list

How can I add the row while keeping the columns (apart from the first one)
as numeric or double or integer?

Thank you, in advance, for your help!

Kind regards
Filippos

	[[alternative HTML version deleted]]



More information about the R-help mailing list