0

I'm using R Version 3.0.2.

Existing Date frame has the following:

>df1
  students....c..Siva....Kumar....Kannan.. Marks....c.450..445..460. students
1                                     Siva                       450     Siva
2                                    Kumar                       445    Kumar
3                                   Kannan                       460   Kannan
       birth
1 1990-08-01
2 1995-07-23
3 1993-12-13

Reference: Already referred What are the "standard unambiguous date" formats? but the format I use is correct.

Error Message

> df1<-rbind(df1, c("Viji",410,"2014-01-23"))
Error in charToDate(x) : 
  character string is not in a standard unambiguous format
In addition: Warning message:
In `[<-.factor`(`*tmp*`, ri, value = "Viji") :
  invalid factor level, NA generated
Community
  • 1
  • 1
Siva Karthikeyan
  • 544
  • 8
  • 25
  • It's trying to coerce your date string to date (or POSIX) but it does not know how. Convert manually, supplying a format – Ricardo Saporta Jan 23 '14 at 15:36
  • 2
    Your data.frame `df1` has four columns and you're trying to add something with 3. So R is recycling the vector you've supplied and trying to coerce `as.Date('Viji')` for the fourth element. `rbind(df1, c('Viji', 410, 'Viji', '2014-01-23'))` should work fine. – Justin Jan 23 '14 at 15:40
  • I tried that and its adding as one of the variable and its giving an warning message "Invalid factor level". Additional Information: Previously I had done this: df1$students<-as.character(df1$students) – Siva Karthikeyan Jan 23 '14 at 15:45

1 Answers1

1

The issue results from the data frame's stringsAsFactors parameter being TRUE by default. When you first declare your data frame, set this parameter to FALSE, and the rbind() function will execute without error.

#Declare some initial vectors:
students = c("Mary", "Jane", "Eva","Leon")
testScores = c(80, 90, 70, 95)
birthDates = c("1990-08-01","1995-07-23","1993-12-13","1990-08-09")

Declare your data frame, and set the stringsAsFactors parameter to FALSE:

df1 <- data.frame (students ,testScores ,birthDates , stringsAsFactors = FALSE)
df1 <- rbind(df1, c("Mike", 87, "2014-02-23"))

The new row should now be attached to the data frame df1:

df1[5,]

results in:

students test1      birth
5     Mike    87 2014-02-23
Mike
  • 100
  • 8