[R] Numeric vector converted mysteriously to characters in data frame?
Martin Ralphs
martin.ralphs at gmail.com
Thu Mar 17 21:18:05 CET 2011
Thanks Jim,
It turns out that the problem was that all columns had been converted
to factors. Once I converted them back to numeric variables the code
worked fine. If anybody is wondering how, you can do this with the
following:
mynumber <- as.numeric(levels(myfactor))[myfactor]
There are plenty of other threads that describe this process fully.
Martin
On Thu, Mar 17, 2011 at 3:11 AM, Jim Holtman <jholtman at gmail.com> wrote:
>
> take a look at what the output of your cbind is; my guess is that it is a character matrix. you probably want to do
>
> data.frame(...,...,...)
>
> without the cbind.
>
> Sent from my iPad
>
> On Mar 16, 2011, at 18:12, Martin Ralphs <martin.ralphs at gmail.com> wrote:
>
> > Dear R Help,
> >
> > I would be very grateful if somebody could explain why this is happening. I
> > am trying to plot a lattice barchart of a vector of numbers with age
> > bandings that I have brought together into a data frame. When I plot the
> > variables in their raw vector form, it works. When I try to plot them in
> > the df, R labels the numeric axis with default factor values and treats the
> > numeric vector as character. Why is this? Apologies if this is blindingly
> > obvious, I am new to R.
> >
> > Here is some sample code that demonstrates the problem:
> >
> > library(lattice)
> >
> > # Load in some sample data ...
> > ageband <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)
> > agenames <-
> > c("00-04","05-09","10-14","15-19","20-24","25-29","30-34","35-39",
> > "40-44","45-49","50-54","55-59","60-64","65-69","70-74","75-79","80-84","85
> > plus")
> > popcount <-
> > c(35274,40958,41574,47973,50384,51248,65748,54854,60948,66473,70854,
> > 60475,61854,55848,45857,30584,25475,20574)
> >
> >
> > region <- "North East"
> > test1 <- as.data.frame(cbind(region,ageband,agenames,popcount))
> > region <- "North West"
> > test2 <- as.data.frame(cbind(region,ageband,agenames,popcount))
> > test <- rbind(test1,test2)
> > names(test) <- c("GOR","Band","AgeBand","Persons")
> >
> > # When I plot my numeric data from the df it is treated as character
> > vector...
> >
> > hg1 <- barchart(AgeBand ~ Persons | GOR,
> > data=test,
> > origin=0,
> > layout=c(2,1),
> > reference=TRUE,
> > xlab="Count (Persons)",
> > ylab="Quinary Age Band",
> > scales="free")
> >
> > hg1
> >
> > # But when I plot the source vectors in the same way, it works how I want it
> > to... (without the region bit, of course)
> > hg2 <- barchart(agenames ~ popcount | region,
> > origin=0,
> > layout=c(2,1),
> > reference=TRUE,
> > xlab="Count (Persons)",
> > ylab="Quinary Age Band",
> > scales="free")
> > hg2
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list