Mixed data type data frames

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

+4
source share
1 answer

You cannot mix types in a vector, so your vector is forced into a symbol.

 R> c("ABCD", 4, "2005-12-12", "2008-04-03 20:30", 3) [1] "ABCD" "4" [3] "2005-12-12" "2008-04-03 20:30" [5] "3" 

[<-.data.frame then forcibly enters the numeric columns of your data.frame file into a character, so the column will be of the same type; although I find it a little inconsistent that it also does not convert Date / POSIXt fields to a character ...

You can mix types in a list. This replacement works because data.frames are the lists below.

 cb[1, ] <- list("ABCD", 4, "2005-12-12", "2008-04-03 20:30", 3) 

When you look back at your code later, it might make sense to replace one line of your data.frame with a 1-line of data.frame:

 cb[1, ] <- data.frame("ABCD", 4, "2005-12-12", "2008-04-03 20:30", 3, stringsAsFactors=FALSE) 
+8
source

All Articles