[R] accessing the attr(*, label.table) after importing from spss

moleps moleps2 at gmail.com
Thu Aug 26 22:03:26 CEST 2010


Thx... The following seems to work. However I´m sure there is a more elegant solution to it....


tre<-dat
b<-length(dat)
 attr(dat,"label.table")->a
 for (i in 1:b){
 	if(!is.null(a[[i]]) & length(levels(as.factor(dat[,i])))==length(a[[i]]))
 	{
 		tre[,i]<-factor(dat[,i],labels=names(a[[i]]))
 		}
}
tre

Regards,

//M



On 25. aug. 2010, at 23.05, Bert Gunter wrote:

> ?attr
> ?attributes
> 
> -- Bert Gunter
> 
> On Wed, Aug 25, 2010 at 1:26 PM, moleps <moleps2 at gmail.com> wrote:
>> Dear all,
>> I just received a file from a colleague in spss. The read.spss could not finish the file due to an error (Unrecognized record type 7, subtype 18 encountered in system file) so instead I converted the file using stat-transfer. Looking at my data I see that most labels are in the attributes and I´d love to access them and assign the pertinent variables to factors without doing the whole factor(levels,labels) thing manually. Is there any remedy to this ??
>> 
>> regards,
>> 
>> M
>> 
>> 
>>> str(dat)
>> 'data.frame':   860 obs. of  19 variables:
>>  $ Ag            : int  15 15 15 15 15 15 15 15 15 15 ...
>>  $ G        : int  2 2 2 1 1 1 1 1 1 2 ...
>>  $ GCQ            : int  15 15 15 15 15 15 15 15 15 15 ...
>>  $ Amn        : int  2 2 2 2 2 2 1 1 1 1 ...
>>  $ HI          : int  1 1 1 1 1 1 2 2 2 2 ...
>>  $ Hos: int  2 2 2 2 2 2 2 2 2 2 ...
>>  $ Risk   : int  2 2 2 2 2 2 2 2 2 2 ...
>>  $ CTO             : int  2 2 2 2 2 1 1 1 1 1 ...
>>  $ pat      : int  NA NA NA NA NA 2 2 2 2 2 ...
>>  $ Day            : int  7 7 7 5 4 6 5 7 7 5 ...
>>  $ Ho      : int  NA NA NA NA NA NA NA NA NA NA ...
>>  $ coh      : int  1 1 1 1 1 1 1 1 1 1 ...
>>  $ Comp     : int  1 1 1 1 1 1 1 1 1 1 ...
>>  $ Ethan        : int  2 2 2 2 2 2 2 2 2 2 ...
>>  $ Pro       : num  NA NA NA NA NA NA NA NA NA NA ...
>>  $ Ye          : int  1 1 1 3 3 3 1 1 1 3 ...
>>  $ Ageg    : int  1 1 1 1 1 1 1 1 1 1 ...
>>  $ BAC            : int  0 0 0 0 0 0 0 0 0 0 ...
>>  - attr(*, "val.labels")= chr  "" "VL_Gender" "" "VL_Amnesia" ...
>>  - attr(*, "var.labels")= chr  "Age (years)" "Gender" "GCQSSA" "Amnesty" ...
>>  - attr(*, "label.table")=List of 19
>>  ..$ : NULL
>>  ..$ : Named num  1 2
>>  .. ..- attr(*, "names")= chr  "Male" "Female"
>>  ..$ : NULL
>>  ..$ : Named num  1 2
>>  .. ..- attr(*, "names")= chr  "Yes" "No"
>>  ..$ : NULL
>>  ..$ : Named num  1 2
>>  .. ..- attr(*, "names")= chr  "Yes" "No"
>>  ..$ : Named num  1 2
>>  .. ..- attr(*, "names")= chr  "Yes" "No"
>>  ..$ : Named num  1 2
>>  .. ..- attr(*, "names")= chr  "Yes" "No"
>>  ..$ : Named num  1 2
>>  .. ..- attr(*, "names")= chr  "Yes" "No"
>>  ..$ : Named num  1 2 3 4 5 6 7
>>  .. ..- attr(*, "names")= chr  "Monday" "Tuesday" "Wednesday" "Thursday" ...
>>  ..$ : Named num  1 2
>>  .. ..- attr(*, "names")= chr  "Yes" "No"
>>  ..$ : Named num  1 2
>>  .. ..- attr(*, "names")= chr  "Yes" "No"
>>  ..$ : Named num  1 2 3 4 5 6 7
>>  .. ..- attr(*, "names")= chr  "Yes" "Overtriage " "Undertriage with admission" "Overtriage with pos" ...
>>  ..$ : Named num  1 2
>>  .. ..- attr(*, "names")= chr  "Yes" "No"
>>  ..$ : NULL
>>  ..$ : NULL
>>  ..$ : Named num  1 2 3 4
>>  .. ..- attr(*, "names")= chr  "15 - 24" "25-39" "40-59" "60-"
>>  ..$ : Named num  1 2 3
>>  .. ..- attr(*, "names")= chr  "0.10-0.99" "1.00-1.99" "2.00-"
>>  ..$ : Named num  0 1 2 3 4
>>  .. ..- attr(*, "names")= chr  "sorry" "undeweight" "0.10-0.99" "1.00-1.99" ...
>> ______________________________________________
>> 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.
>> 



More information about the R-help mailing list