Re: [R] Numeric vector converted mysteriously to characters in data frame?

From: Jim Holtman <jholtman_at_gmail.com>
Date: Wed, 16 Mar 2011 23:11:15 -0400

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.



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. Received on Thu 17 Mar 2011 - 03:11:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 17 Mar 2011 - 20:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive