I have been using R for a little while, but I am still struggling with factors and data frames. Here's my question.
I am trying to pre-allocate a data frame composed 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 gets met the data frame types that I want (output of str(cb)):
'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 item in the data frame, CC and I become characters:
cb[1, ] <- c("ABCD", 4, "2005-12-12", "2008-04-03 20:30", 3)
output of 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 rather unusable for my purposes.
When I omit stringsAsFactors=FALSE in the data.frame definition, I (obviously) get a different error message (having set warn to 2):
Error in `[<-.factor`(`*tmp*`, iseq, value = "ABCD") :
(converted from warning) invalid factor level, NAs generated
which I understand but I am not sure how to overcome either.
What am I doing wrong? How can I make sure to keep the numeric type for columns I and SD? Thanks so much for your help.
Cheers
B