Stataq
Stataq

Reputation: 2301

how to get data attr into a table

I am reading in a sas data file would like to get attr. If I use haven, I looks like I can only keep "labels". If I use read.sas7bdat, it looks like I can get more attr.

enter image description here

Could any one tell me how I can get attr into a table? If the imported a SAS data into R as df, then I can get variables and labels into a table like below:

tibble(
  Variable = df %>% names(),
  Labels = df %>% map_chr(attr_getter("label"))
)

Any suggestion on how to get two more cols: Length and type?

Upvotes: 1

Views: 63

Answers (1)

akrun
akrun

Reputation: 886948

We may use attributes to return a list of named attributes and stack it to a two column data.frame

lst1 <- lapply(attributes(df)$column.info, function(x) 
     stack(x[c("label", "length", "type")])[2:1])
out <- do.call(rbind, Map(cbind, id = seq_along(lst1), lst1))

-output

> head(out)
  id    ind              values
1  1  label    Study Identifier
2  1 length                 200
3  1   type           character
4  2  label Domain Abbreviation
5  2 length                   2
6  2   type           character
> tail(out)
   id    ind                  values
28 10  label Date/Time of Collection
29 10 length                      19
30 10   type               character
31 11  label Study Day of Collection
32 11 length                       8
33 11   type                 numeric

Upvotes: 1

Related Questions