I have been using R for a while, but I'm still scared with factors and data frames. Here is my question.
I am trying to pre-allocate a data frame consisting of several columns of different types, as follows:
cb <- data.frame(S=character(1000), I=numeric(1000), A=as.Date(rep(0,1000), origin = "1900-01-01"), SD=as.POSIXct(rep(0,1000), origin = "1900-01-01 00:00:00"), CC=numeric(1000), stringsAsFactors=FALSE)
which encounters the data types that I want (str (cb) output):
'data.frame': 1000 obs. of 5 variables: $ S : chr "" "" "" "" ... $ I : num 0 0 0 0 0 0 0 0 0 0 ... $ A : Date, format: "1900-01-01" "1900-01-01" "1900-01-01" "1900-01-01" ... $ SD: POSIXct, format: "1900-01-01" "1900-01-01" "1900-01-01" "1900-01-01" ... $ CC: num 0 0 0 0 0 0 0 0 0 0 ...
When I assign the first element in a data frame, CC and I become characters:
cb[1, ] <- c("ABCD", 4, "2005-12-12", "2008-04-03 20:30", 3)
output str (cb):
'data.frame': 1000 obs. of 5 variables: $ S : chr "ABCD" "" "" "" ... $ I : chr "4" "0" "0" "0" ... $ A : Date, format: "2005-12-12" "1900-01-01" "1900-01-01" "1900-01-01" ... $ SD: POSIXct, format: "2008-04-03 20:30:00" "1900-01-01 00:00:00" "1900-01-01 00:00:00" "1900-01-01 00:00:00" ... $ CC: chr "3" "0" "0" "0" ...
which makes it pretty unsuitable for my purposes.
When I omit stringsAsFactors = FALSE in the definition of data.frame, I (obviously) get another error message (setting warning to 2):
Error in `[<-.factor`(`*tmp*`, iseq, value = "ABCD") : (converted from warning) invalid factor level, NAs generated
which I understand, but I'm not sure how to overcome this.
What am I doing wrong? How can I save the numeric type for columns i and sd? Many thanks for your help.
Greetings
IN