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?
--
View this message in context: http://r.789695.n4.nabble.com/unwanted-coercion-using-apply-tp3541637p3541637.html
Sent from the R help mailing list archive at Nabble.com.
unwanted coercion using apply
6 messages · Peter Ehlers, Jeroen Ooms, David Winsemius
On 2011-05-21 23:11, 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:
But it does; see below;
as.list(mydata[1,]);
This avoids apply. Try: apply(mydata[1,], 1, as.list) and you'll get the conversion of numerics to strings. apply() operates on arrays and a data frame will be coerced to a matrix (which requires all elements to be of the same type). This is documented in ?apply.
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?
This may not be elegant, but why not just use a loop:
L <- vector("list", 3)
for( i in 1:3 ) L[[i]] <- as.list(mydata[i, ])
Peter Ehlers
-- View this message in context: http://r.789695.n4.nabble.com/unwanted-coercion-using-apply-tp3541637p3541637.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110522/ca09d36a/attachment.pl>
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
On May 22, 2011, at 3:22 PM, Jeroen Ooms wrote:
apply() operates on arrays and a data frame will be coerced to a matrix (which requires all elements to be of the same type). This is documented in ?apply.
Thanks, I was not aware of that. I implicitly assumed there would be a specific apply.data.frame.
Because there is a constraint in R that all elements of a vector need to be of the same type.
This may not be elegant, but why not just use a loop:
The thing is that were are going to use this operation on a (very) large scale in a production environment, on datasets for which nrow >> ncol. So I would like the most efficient solution and if possibly, avoid looping over the rows.
But `apply` loops over rows, too. No speed advantage is being gained. You may get better answers if you post a representative example that has types on which you will be operating. There may be vectorizable solutions that can operate column-wise.
Thanks
David Winsemius, MD West Hartford, CT
On May 22, 2011, at 3:40 PM, David Winsemius wrote:
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)
I meant to preface that by noting that your dataframe has a factor
column and I suspect you want it to be "character", so:
ydata <- data.frame(foo=1:3, bar=c("M","M","F"),
stringsAsFactors=FALSE)
pairlist(mydata$bar, ydata$foo) # assuming the numbers were values for letter names.
David Winsemius, MD West Hartford, CT