[R] More issues with apply

David Winsemius dwinsemius at comcast.net
Sat Jan 14 04:34:47 CET 2012


On Jan 13, 2012, at 4:55 PM, LCOG1 wrote:

> Hi all,
>     Please consider the following:
>
> DataSet1.. <-
> data
> .frame(Bldgtype=c("SFDM","SFDM","SFDM","SFDM","SFDM","SFDM","APT"),
> Taz=c("254","254","254","564","564","564","564"),stringsAsFactors =  
> FALSE)
> PeriodResType_Zx <- tapply(as.character(DataSet1..$Bldgtype),
> as.character(DataSet1..$Taz), table)
> PeriodResType_Zx
>
> $`254`
>
> SFDM
>   3
>
> $`564`
>
> APT SFDM
>   1    3
>
> DataSet2.. <-
> data.frame(Bldgtype=c("SFDM","SFDM","SFDM","SFDM","SFDM","SFDM"),
> Taz=c("254","245","564","564","564","564"))
> PeriodResType_Zx <- tapply(DataSet2..$Bldgtype, DataSet2..$Taz, table)
> PeriodResType_Zx
>
> Data set 1 returns the desired outcome, an array with elements  
> consisting of
> a labeled table
>
> Data set 2 returns an array without the table labeled.
>
> This is occuring becuase there are not a variety of Bldgtypes in  
> data set
> two, but this is a reality of the data so how can i get the data  
> into the
> same format, with a Bldgtype descriptor in the array like the Data  
> set 1
> result?

You do not actually say what sort of output you want (other than by  
analogybut try using this as the first column in your data.frame call:

Bldgtype 
=factor(c("SFDM","SFDM","SFDM","SFDM","SFDM","SFDM") ,levels=c("SFDM",  
"APT"))

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list