Michael Friendly
2010-Oct-25 15:45 UTC
[R] extracting characteristics of datasets from data()
I can use data() to find the available datasets in a package, but I'd like to extract and display some additional information for each dataset than what is provided by data(), e.g., class() and dim() for datasets for which these are available. I'm stuck on using using lapply properly with objects, rather than the names of objects that I get from data() Example: > DS <- data(package="vcdExtra") > DS$results[,c("Item", "Title")] Item Title [1,] "Abortion" "Abortion Opinion Data" [2,] "Bartlett" "Bartlett data on plum root cuttings" [3,] "Caesar" "Risk Factors for Infection in Caesarian Births" [4,] "Cancer" "Survival of Breast Cancer Patients" [5,] "Detergent" "Detergent preference data" [6,] "Dyke" "Sources of knowledge of cancer" [7,] "GSS" "General Social Survey-- Sex and Party affiliation" [8,] "Gilby" "Clothing and Intelligence Rating of Children" [9,] "Heart" "Sex, Occupation and Heart Disease" [10,] "Heckman" "Labour Force Participation of Married Women 1967-1971" [11,] "Hoyt" "Minnesota High School Graduates" [12,] "ICU" "Death in the ICU" [13,] "JobSat" "Cross-classification of job satisfaction by income" [14,] "Mental" "Mental impariment and parents SES" [15,] "Mobility" "Social Mobility data" [16,] "TV" "TV Viewing Data" [17,] "Vietnam" "Student Opinion About the War in Vietnam" [18,] "Yamaguchi87" "Occupational Mobility in Three Countries" > unlist(lapply(as.list(DS$results[,c("Item") ]), FUN=class)) [1] "character" "character" "character" "character" "character" "character" "character" [8] "character" "character" "character" "character" "character" "character" "character" [15] "character" "character" "character" "character" Wanted: something like the results of doing > class(Abortion) [1] "table" > dim(Abortion) [1] 2 2 2 > class(Mental) [1] "data.frame" > dim(Mental) [1] 24 3 > for all datasets in Item, giving a display like Item class dim Title Abortion table 2x2x2 Abortion Opinion Data Mental data.frame 24x2 Mental impariment and parents SES -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele Street Web: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA
Seeliger.Curt at epamail.epa.gov
2010-Oct-25 16:16 UTC
[R] extracting characteristics of datasets from data()
> Michael writes: > I can use data() to find the available datasets in a package, but I'd > like to extract and display some additional > information for each dataset than what is provided by data(), e.g., > class() and dim() for datasets for which > these are available. > ... > for all datasets in Item, giving a display like > > Item class dim Title > Abortion table 2x2x2 Abortion Opinion Data > Mental data.frame 24x2 Mental impariment and parents SESThis seems to work on your example: require('vcdExtra') ds <- as.data.frame(data(package="vcdExtra")$results[,c('Item','Title')], stringsAsFactors=FALSE) ds$dim <- lapply(ds$Item, function(x) { paste(dim(get(x)), collapse='x') } ) ds$class <- lapply(ds$Item, function(x) { class(get(x)) } ) ds <- ds[c('Item','class','dim','Title')] Thanks for the fun exercise. I've just started looking at the various 'apply-oid' functions, having spent much of my time with aggregate() and perhaps having my imagination stunted. cur -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.curt@epa.gov 541/754-4638 [[alternative HTML version deleted]]
Henrique Dallazuanna
2010-Oct-25 17:36 UTC
[R] extracting characteristics of datasets from data()
Try this: data(list = DS$results[,'Item'], package = 'vcdExtra', envir = ne <- new.env()) cbind(do.call(rbind, eapply(ne, function(x)data.frame(class = class(x), dim = paste(dim(x), collapse = 'x')))), Title = DS$results[,'Title']) On Mon, Oct 25, 2010 at 1:45 PM, Michael Friendly <friendly@yorku.ca> wrote:> I can use data() to find the available datasets in a package, but I'd like > to extract and display some additional > information for each dataset than what is provided by data(), e.g., > class() and dim() for datasets for which > these are available. I'm stuck on using using lapply properly with > objects, rather than the names of objects > that I get from data() > > Example: > > > DS <- data(package="vcdExtra") > > DS$results[,c("Item", "Title")] > Item Title > [1,] "Abortion" "Abortion Opinion Data" > [2,] "Bartlett" "Bartlett data on plum root cuttings" > [3,] "Caesar" "Risk Factors for Infection in Caesarian Births" > [4,] "Cancer" "Survival of Breast Cancer Patients" > [5,] "Detergent" "Detergent preference data" > [6,] "Dyke" "Sources of knowledge of cancer" > [7,] "GSS" "General Social Survey-- Sex and Party affiliation" > [8,] "Gilby" "Clothing and Intelligence Rating of Children" > [9,] "Heart" "Sex, Occupation and Heart Disease" > [10,] "Heckman" "Labour Force Participation of Married Women 1967-1971" > [11,] "Hoyt" "Minnesota High School Graduates" > [12,] "ICU" "Death in the ICU" > [13,] "JobSat" "Cross-classification of job satisfaction by income" > [14,] "Mental" "Mental impariment and parents SES" > [15,] "Mobility" "Social Mobility data" > [16,] "TV" "TV Viewing Data" > [17,] "Vietnam" "Student Opinion About the War in Vietnam" > [18,] "Yamaguchi87" "Occupational Mobility in Three Countries" > > unlist(lapply(as.list(DS$results[,c("Item") ]), FUN=class)) > [1] "character" "character" "character" "character" "character" > "character" "character" > [8] "character" "character" "character" "character" "character" > "character" "character" > [15] "character" "character" "character" "character" > > Wanted: something like the results of doing > > > class(Abortion) > [1] "table" > > dim(Abortion) > [1] 2 2 2 > > class(Mental) > [1] "data.frame" > > dim(Mental) > [1] 24 3 > > > for all datasets in Item, giving a display like > > Item class dim Title > Abortion table 2x2x2 Abortion Opinion Data > Mental data.frame 24x2 Mental impariment and parents SES > > -- > Michael Friendly Email: friendly AT yorku DOT ca > Professor, Psychology Dept. > York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 > 4700 Keele Street Web: http://www.datavis.ca > Toronto, ONT M3J 1P3 CANADA > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]