gather_attrs moves variable-level attributes to the data frame level and spread_attrs reverses that operation.

gather_attrs(x)

spread_attrs(x)

Arguments

x

A data frame.

Value

x, with variable-level attributes stored at the data frame level.

Details

import attempts to standardize the return value from the various import functions to the extent possible, thus providing a uniform data structure regardless of what import package or function is used. It achieves this by storing any optional variable-related attributes at the variable level (i.e., an attribute for mtcars$mpg is stored in attributes(mtcars$mpg) rather than attributes(mtcars)). gather_attrs moves these to the data frame level (i.e., in attributes(mtcars)). spread_attrs moves attributes back to the variable level.

See also

Examples

e <- import("http://www.stata-press.com/data/r13/auto.dta") str(e)
#> 'data.frame': 74 obs. of 12 variables: #> $ make : chr "AMC Concord" "AMC Pacer" "AMC Spirit" "Buick Century" ... #> ..- attr(*, "label")= chr "Make and Model" #> ..- attr(*, "format.stata")= chr "%-18s" #> $ price : num 4099 4749 3799 4816 7827 ... #> ..- attr(*, "label")= chr "Price" #> ..- attr(*, "format.stata")= chr "%8.0gc" #> $ mpg : num 22 17 22 20 15 18 26 20 16 19 ... #> ..- attr(*, "label")= chr "Mileage (mpg)" #> ..- attr(*, "format.stata")= chr "%8.0g" #> $ rep78 : num 3 3 NA 3 4 3 NA 3 3 3 ... #> ..- attr(*, "label")= chr "Repair Record 1978" #> ..- attr(*, "format.stata")= chr "%8.0g" #> $ headroom : num 2.5 3 3 4.5 4 4 3 2 3.5 3.5 ... #> ..- attr(*, "label")= chr "Headroom (in.)" #> ..- attr(*, "format.stata")= chr "%6.1f" #> $ trunk : num 11 11 12 16 20 21 10 16 17 13 ... #> ..- attr(*, "label")= chr "Trunk space (cu. ft.)" #> ..- attr(*, "format.stata")= chr "%8.0g" #> $ weight : num 2930 3350 2640 3250 4080 3670 2230 3280 3880 3400 ... #> ..- attr(*, "label")= chr "Weight (lbs.)" #> ..- attr(*, "format.stata")= chr "%8.0gc" #> $ length : num 186 173 168 196 222 218 170 200 207 200 ... #> ..- attr(*, "label")= chr "Length (in.)" #> ..- attr(*, "format.stata")= chr "%8.0g" #> $ turn : num 40 40 35 40 43 43 34 42 43 42 ... #> ..- attr(*, "label")= chr "Turn Circle (ft.) " #> ..- attr(*, "format.stata")= chr "%8.0g" #> $ displacement: num 121 258 121 196 350 231 304 196 231 231 ... #> ..- attr(*, "label")= chr "Displacement (cu. in.)" #> ..- attr(*, "format.stata")= chr "%8.0g" #> $ gear_ratio : num 3.58 2.53 3.08 2.93 2.41 ... #> ..- attr(*, "label")= chr "Gear Ratio" #> ..- attr(*, "format.stata")= chr "%6.2f" #> $ foreign : num 0 0 0 0 0 0 0 0 0 0 ... #> ..- attr(*, "label")= chr "Car type" #> ..- attr(*, "format.stata")= chr "%8.0g" #> ..- attr(*, "labels")= Named num 0 1 #> .. ..- attr(*, "names")= chr "Domestic" "Foreign" #> - attr(*, "label")= chr "1978 Automobile Data" #> - attr(*, "notes")= chr "from Consumer Reports with permission" "1"
g <- gather_attrs(e) str(attributes(e))
#> List of 5 #> $ label : chr "1978 Automobile Data" #> $ notes : chr [1:2] "from Consumer Reports with permission" "1" #> $ names : chr [1:12] "make" "price" "mpg" "rep78" ... #> $ row.names: int [1:74] 1 2 3 4 5 6 7 8 9 10 ... #> $ class : chr "data.frame"
str(g)
#> 'data.frame': 74 obs. of 12 variables: #> $ make : chr "AMC Concord" "AMC Pacer" "AMC Spirit" "Buick Century" ... #> $ price : num 4099 4749 3799 4816 7827 ... #> $ mpg : num 22 17 22 20 15 18 26 20 16 19 ... #> $ rep78 : num 3 3 NA 3 4 3 NA 3 3 3 ... #> $ headroom : num 2.5 3 3 4.5 4 4 3 2 3.5 3.5 ... #> $ trunk : num 11 11 12 16 20 21 10 16 17 13 ... #> $ weight : num 2930 3350 2640 3250 4080 3670 2230 3280 3880 3400 ... #> $ length : num 186 173 168 196 222 218 170 200 207 200 ... #> $ turn : num 40 40 35 40 43 43 34 42 43 42 ... #> $ displacement: num 121 258 121 196 350 231 304 196 231 231 ... #> $ gear_ratio : num 3.58 2.53 3.08 2.93 2.41 ... #> $ foreign : num 0 0 0 0 0 0 0 0 0 0 ... #> ..- attr(*, "labels")= Named num 0 1 #> .. ..- attr(*, "names")= chr "Domestic" "Foreign" #> - attr(*, "title")= chr "1978 Automobile Data" #> - attr(*, "notes")= chr "from Consumer Reports with permission" "1" #> - attr(*, "format.stata")=List of 12 #> ..$ make : chr "%-18s" #> ..$ price : chr "%8.0gc" #> ..$ mpg : chr "%8.0g" #> ..$ rep78 : chr "%8.0g" #> ..$ headroom : chr "%6.1f" #> ..$ trunk : chr "%8.0g" #> ..$ weight : chr "%8.0gc" #> ..$ length : chr "%8.0g" #> ..$ turn : chr "%8.0g" #> ..$ displacement: chr "%8.0g" #> ..$ gear_ratio : chr "%6.2f" #> ..$ foreign : chr "%8.0g" #> - attr(*, "label")=List of 12 #> ..$ make : chr "Make and Model" #> ..$ price : chr "Price" #> ..$ mpg : chr "Mileage (mpg)" #> ..$ rep78 : chr "Repair Record 1978" #> ..$ headroom : chr "Headroom (in.)" #> ..$ trunk : chr "Trunk space (cu. ft.)" #> ..$ weight : chr "Weight (lbs.)" #> ..$ length : chr "Length (in.)" #> ..$ turn : chr "Turn Circle (ft.) " #> ..$ displacement: chr "Displacement (cu. in.)" #> ..$ gear_ratio : chr "Gear Ratio" #> ..$ foreign : chr "Car type" #> - attr(*, "labels")=List of 12 #> ..$ make : NULL #> ..$ price : NULL #> ..$ mpg : NULL #> ..$ rep78 : NULL #> ..$ headroom : NULL #> ..$ trunk : NULL #> ..$ weight : NULL #> ..$ length : NULL #> ..$ turn : NULL #> ..$ displacement: NULL #> ..$ gear_ratio : NULL #> ..$ foreign : Named num 0 1 #> .. ..- attr(*, "names")= chr "Domestic" "Foreign"