Expected behavior of as.character ??

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Expected behavior of as.character ??

koooee

is this the expected behavior of as.character ?

resultset is a data.frame from a sqlQuery() using RODBC

> a = as.character(as.vector(resultset[1]))
> a
[1] "c(-1, 1, 2, 3, 4, 5, 6, 7, 8, 9)"

I would expect the statement above to return similar to the result below, am
I missing something?

> as.character(c(-1,1,2,3,4,5,6,7,8,9))
 [1] "-1" "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"
--
View this message in context: http://r.789695.n4.nabble.com/Expected-behavior-of-as-character-tp3297558p3297558.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Expected behavior of as.character ??

Erik Iverson-3


koooee wrote:

> is this the expected behavior of as.character ?
>
> resultset is a data.frame from a sqlQuery() using RODBC
>
>> a = as.character(as.vector(resultset[1]))
>> a
> [1] "c(-1, 1, 2, 3, 4, 5, 6, 7, 8, 9)"
>
> I would expect the statement above to return similar to the result below, am
> I missing something?
>
>> as.character(c(-1,1,2,3,4,5,6,7,8,9))
>  [1] "-1" "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"

What class is resultset[1] ? A list, yes? Thus the behavior.
try


 > as.character(resultset[[1]])

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Expected behavior of as.character ??

Duncan Murdoch-2
In reply to this post by koooee
On 09/02/2011 11:11 AM, koooee wrote:

> is this the expected behavior of as.character ?
>
> resultset is a data.frame from a sqlQuery() using RODBC
>
> >  a = as.character(as.vector(resultset[1]))
> >  a
> [1] "c(-1, 1, 2, 3, 4, 5, 6, 7, 8, 9)"
>
> I would expect the statement above to return similar to the result below, am
> I missing something?

Yes, you should look at what

as.vector(resultset[1])

gives you.  I think it is not what you expected.  (Use str() to see that
it is still a data frame).

Duncan Murdoch

> >  as.character(c(-1,1,2,3,4,5,6,7,8,9))
>   [1] "-1" "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"

______________________________________________
[hidden email] 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.