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

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sun Sep 4 17:20:06 CEST 2016


The "c" function creates vectors. Rows of data frames are data frames, not vectors.

new_row  <- data.frame( Prod_name = "Day_name",  `Date 1`=1, `Date 2`=2,`Date 3`=3 )
data_may  <- rbind( new_row, data_may )

Furthermore, data frames are NOT spreadsheets. "Day_num" looks suspiciously UNlike a product name, which may mean the corresponding values in that row are not Dates, which would also lead you into trouble.

Please read the Posting Guide. In particular, you should read about making your examples reproducible. Part of that is posting in plain text and using the dput function to give us your sample data, because all too often the problem lies in the details of how you have imported and manipulated your data and the shortest way for us to see that the data are okay is to see it as it exists in your R script so far.
-- 
Sent from my phone. Please excuse my brevity.

On September 4, 2016 6:22:48 AM PDT, Filippos Katsios <katsiosf at gmail.com> wrote:
>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]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list