[R] unwanted as.integer

Henrik Bengtsson
Fri, 30 Jul 2004 07:16:27 -0000

I suspect sqlQuery() returns a data frame, is that correct? Then try to
convert it into a matrix before you paste() it. Example:

> x <- data.frame(a=1:3, b=1:3+0.5)
> as.character(x)
[1] "as.integer(c(1, 2, 3))" "c(1.5, 2.5,
3.5)"
> as.character(as.matrix(x))
[1] "1"   "2"   "3"   "1.5"
"2.5" "3.5"

If it returns a list you can do:

> x <- list(a=1:3, b=1:3+0.5)
> as.character(x)
[1] "as.integer(c(1, 2, 3))" "c(1.5, 2.5, 3.5)"

> lapply(x, FUN=as.character)
$a
[1] "1" "2" "3"

$b
[1] "1.5" "2.5" "3.5"

Hope this helps

Henrik Bengtsson

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jack Tanner
> Sent: Thursday, July 29, 2004 11:55 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] unwanted as.integer
>
>
>  > a <- sqlQuery(irrdb, "select count(field) from mytable
> where field = 1")  > print(a)
>   count(field)
> 1            8
>  > paste(a)
> [1] "as.integer(8)"
>
> Why the as.integer() representation? I later pass the result
> into this
> write.html.table(), and what I get is rows of as.integer()...
> when all I
> want is the integer itself.
>
> as.integer(31) 	as.integer(21) 	as.integer(34) 	as.integer(86)
> as.integer(7) 	as.integer(6) 	as.integer(15) 	as.integer(28)
>
>
> write.html.table <- function(t, file= "", capt= "The
Default Title",
>                             append = FALSE) {
> head <- paste(" \n ", capt,
" \n")
>   cat(head, file= file, append= append)
>   if (is.null(rownames(t))) {
>     rownames(t) <- rep("", nrow(t))
>   }
> tp <- rbind(c(" ",
colnames(t)),
>               cbind(unlist(lapply(rownames(t), function(x) {
> paste(" ", x)})), t))
> write.table(tp, sep= " ", file= file, , eol=
" \n",
>               row.names=FALSE,
>               col.names=FALSE,
>               append=TRUE, quote=FALSE)
> cat(" \n \n", file= file, append= TRUE)
> }
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html