I´m trying to get a lot of regression coefficients into a dataframe for latex´ing afterwards. However, I´m running into the following problem that I cannot understand after pasting together some values into confidence intervals:
> str(q2)
'data.frame': 5 obs. of 7 variables:
$ name : Factor w/ 5 levels "1","2",..: 1 2 3 4 5
$ Intercept: Factor w/ 5 levels "15.4533848220452",..: 1 2 3 4 5
$ Int.lb : Factor w/ 5 levels "14.2125590292247",..: 1 2 3 4 5
$ Int.ub : Factor w/ 5 levels "17.1483176230248",..: 1 2 3 4 5
$ BAC : Factor w/ 5 levels "-0.317030740768092",..: 1 2 3 4 5
$ Bac.lb : Factor w/ 5 levels "-0.789518593140102",..: 1 2 3 4 5
$ Bac.ub : Factor w/ 5 levels "0.0844578956839408",..: 1 2 3 4 5
> str(q3)
'data.frame': 5 obs. of 2 variables:
$ CI: Factor w/ 5 levels "(12.17,14.34)",..: 2 1 5 4 3
$ ci: Factor w/ 5 levels "(-0.31,0.74)",..: 3 5 2 4 1
> q4<-as.data.frame(cbind(name=q2$name,Intercept=q2$Intercept,Interecpt.95.CI=q3$CI,BAC=q2$BAC,BAC.95.CI=q3$ci))
> q4
name Intercept Interecpt.95.CI BAC BAC.95.CI
1 1 1 2 1 3
2 2 2 1 2 5
3 3 3 5 3 2
4 4 4 4 4 4
5 5 5 3 5 1
> str(q4)
'data.frame': 5 obs. of 5 variables:
$ name : int 1 2 3 4 5
$ Intercept : int 1 2 3 4 5
$ Interecpt.95.CI: int 2 1 5 4 3
$ BAC : int 1 2 3 4 5
$ BAC.95.CI : int 3 5 2 4 1
I.e. Why did the q4 variables all of a sudden change?
The short answer is the factors got converted to their internal numeric codes. It happened during the
cbind()call:The reason is that
cbind()produces a matrix and that is where the conversion happens. It would be easier to create a new data frame in this instance:rather than a
cbind()followed by anas.data.frame()pair of calls.But the real source of the problem is the numeric data stored as a factor in
q2. How were these data read in or generated in R? If the were read in to R, why do the end up as a factor? Usually is the data are all numeric in a column R will read in the values as numerics. If there is anything text-like in the data column though, it will get converted to a factor. So I’d try to solve that issue – why were the data inq2factors – as it might indicate some issues with reading or generating the data that you are not aware of.