Re: [R] unwanted coercion using apply

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Sun, 22 May 2011 15:40:06 -0400

On May 22, 2011, at 2:11 AM, Jeroen Ooms wrote:

> I need to convert a dataframe to a record-structure, to be able to
> encode it
> later in JSON. Suppose this is the data:
>
> mydata <- data.frame(foo=1:3, bar=c("M","M","F"));
>
> I would like to convert this to a unnamed list (json array) of key-
> value
> pairs. For example like this:
>
> apply(data.frame(foo=1:3, bar=c("M","M","F")),1,as.list)
>
> However, when I do this, all the numeric values are converted to
> strings. I
> don't understand why this is, because when I try to convert one record
> simulataniously, this does not happen:
>
> as.list(mydata[1,]);
>
> I am not sure if this is indended behaviour or not, but is there an
> elegant
> way to apply 'as.list' to all of the dataframe rows without coercing
> everything to strings?

?pairlist

pairlist( letters[1:3], 1:3)

pairlist(mydata$bar, ydata$foo) # assuming the numbers were values for letter names.

-- 

David Winsemius, MD
West Hartford, CT

______________________________________________
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 Sun 22 May 2011 - 19:43:04 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

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 Sun 22 May 2011 - 20:00:08 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