[R] Operating on RC in a list

Mohan.Radhakrishnan at cognizant.com Mohan.Radhakrishnan at cognizant.com
Thu Jun 8 12:02:03 CEST 2017


I am replying to my question.
AFAIK dplyr works only with data frames.

So I flattened the RC's like this. A pure OO approach and a functional representation of it are at loggerheads. I think.

                                  filteredmeasurements <-
                                  keep(measurements, function(x){
                                                       x$getid() == subject$getid()
                                                       })
                                  groupedmeasurements <-
                                         filteredmeasurements %>% lapply(function(x){
                                                       m <<- x$getmeasurement()
                                                       as.data.frame(list('visit'=m$getvisit(),
                                                                                      'location'=x$getlocation()$getlocation(),
                                                                                         'amount'=m$getquantity()$amount))
                                                }) %>% rbind_all()
                                  dataColumns <- c('amount')
                                  ddply(groupedmeasurements,c('visit','location'),function(x) colSums(x[dataColumns]))



Thanks,
Mohan

From: Radhakrishnan, Mohan (Cognizant)
Sent: Wednesday, June 07, 2017 2:05 PM
To: r-help at r-project.org
Subject: Operating on RC in a list

Hi,

I have a hierarchy of such classes. Subject has a list of measurements. Let assume I have a list of such 'Subject' RC's.

Can I use dplyr to navigate from Subject to the list of measurement RC's and filter and group data ? dplyr should
be able to call the methods on these RC's to operate on the data structure ?

I tried to coerce the list of RC's into a data frame unsuccessfully. But dplyr should be able to work with lists too. Right ?


Subject <- setRefClass("Subject",
fields = list( id = "numeric",
measurement = "Measurement",
location = "Location"),
methods=list(getmeasurement = function()
{
measurement
},
getid = function()
{
id
},
getlocation = function()
{
location
},
summary = function()#Implement other summary methods in appropriate objects as per their responsibilities
{
paste("Subject summary ID [",id,"] Location [",location$summary(),"]")
},show = function(){
cat("Subject summary ID [",id,"] Location [",location$summary(),"]\n")
})
)


Thanks,
Mohan
This e-mail and any files transmitted with it are for the sole use of the intended recipient(s) and may contain confidential and privileged information. If you are not the intended recipient(s), please reply to the sender and destroy all copies of the original message. Any unauthorized review, use, disclosure, dissemination, forwarding, printing or copying of this email, and/or any action taken in reliance on the contents of this e-mail is strictly prohibited and may be unlawful. Where permitted by applicable law, this e-mail and other e-mail communications sent to and from Cognizant e-mail addresses may be monitored.

	[[alternative HTML version deleted]]



More information about the R-help mailing list