Looping through labelles to create them in the same way - r

I really like the labelled package.
I have an analysis that has tons of labels that I need to make. Instead of adding them one by one, is there a way to have it loop through all columns and to modify them in the same way. For example, if I wanted to make them all Title Case. Please note, I'm hoping to change the label, not the actual column name.
library(labelled)
library(ggplot2)
mpg_new <- ggplot2::mpg %>%
set_variable_labels(manufacturer = "Manufacturer")
labelled::var_label(mpg_new$manufacturer)

If we need to convert to title case on all of them, can pass a named vector as well in set_variable_labels
library(labelled)
library(ggplot2)
data(mpg)
var_labels <- setNames(tools::toTitleCase(names(mpg)), names(mpg))
mpg_new <- mpg %>%
set_variable_labels(.labels = var_labels, .strict = FALSE)
-checking
> str(mpg_new)
tibble [234 × 11] (S3: tbl_df/tbl/data.frame)
$ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
..- attr(*, "label")= chr "Manufacturer"
$ model : chr [1:234] "a4" "a4" "a4" "a4" ...
..- attr(*, "label")= chr "Model"
$ displ : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
..- attr(*, "label")= chr "Displ"
$ year : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
..- attr(*, "label")= chr "Year"
$ cyl : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
..- attr(*, "label")= chr "Cyl"
$ trans : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
..- attr(*, "label")= chr "Trans"
$ drv : chr [1:234] "f" "f" "f" "f" ...
..- attr(*, "label")= chr "Drv"
$ cty : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
..- attr(*, "label")= chr "Cty"
$ hwy : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
..- attr(*, "label")= chr "Hwy"
$ fl : chr [1:234] "p" "p" "p" "p" ...
..- attr(*, "label")= chr "Fl"
$ class : chr [1:234] "compact" "compact" "compact" "compact" ...
..- attr(*, "label")= chr "Class"

Another option to achieve your desired result would be via labelled::var_label like so:
library(labelled)
library(ggplot2)
mpg_new <- ggplot2::mpg
var_label(mpg_new) <- stringr::str_to_title(names(mpg_new))
var_label(mpg_new, unlist = TRUE)
#> manufacturer model displ year cyl
#> "Manufacturer" "Model" "Displ" "Year" "Cyl"
#> trans drv cty hwy fl
#> "Trans" "Drv" "Cty" "Hwy" "Fl"
#> class
#> "Class"

Related

R: how to use user-defined function on my data [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am carrying out data analysis using R but have some problem with coding.
I create my own function for creating frequency table and apply it to variables in my data, but R shows error message.
Could anyone can give me any solution and why it did not work?
> str(diabetes)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 56632 obs. of 30 variables:
$ ID : chr "A308059801" "A308059802" "A308120201" "A308120202" ...
$ year : num 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010 ...
$ region : num 1 1 1 1 1 1 1 1 1 1 ...
$ sex : num 1 2 1 2 2 1 2 1 2 1 ...
$ age : num 61 54 33 33 4 65 59 54 49 18 ...
$ edu : chr "3.000000" "2.000000" "3.000000" "4.000000" ...
$ occp : chr "5.000000" "3.000000" "4.000000" "1.000000" ...
$ marri_1 : 'labelled' num 1 1 1 1 2 1 1 1 1 2 ...
..- attr(*, "label")= chr "Marriage Y/N"
$ marri_2 : 'labelled' num 1 1 1 1 8 1 1 1 1 8 ...
..- attr(*, "label")= chr "Marriage status"
$ tins : 'labelled' num 10 20 10 10 10 20 20 10 10 10 ...
..- attr(*, "label")= chr "Insurance registration"
$ D_1_1 : 'labelled' chr "3.000000" "2.000000" "2.000000" "3.000000" ...
..- attr(*, "label")= chr "Self-report health status"
$ DI1_dg : 'labelled' chr "1.000000" "8.000000" "8.000000" "8.000000" ...
..- attr(*, "label")= chr "HBP diagnosis"
$ DI1_pr : 'labelled' chr "1.000000" "8.000000" "8.000000" "8.000000" ...
..- attr(*, "label")= chr "HBP current status"
$ DI1_pt : 'labelled' chr "1.000000" "8.000000" "8.000000" "8.000000" ...
..- attr(*, "label")= chr "HBP care"
$ DE1_dg : 'labelled' chr "8.000000" "8.000000" "8.000000" "8.000000" ...
..- attr(*, "label")= chr "Diabetes diagnosis"
$ DE1_pr : 'labelled' chr "8.000000" "8.000000" "8.000000" "8.000000" ...
..- attr(*, "label")= chr "Diabetes status"
$ DE1_pt : 'labelled' chr "8.000000" "8.000000" "8.000000" "8.000000" ...
..- attr(*, "label")= chr "Diabetes cure"
$ HE_DMdg : 'labelled' chr "0.000000" "0.000000" "0.000000" "0.000000" ...
..- attr(*, "label")= chr "Diabetes doctor diagnosis"
$ HE_BMI : 'labelled' chr "26.177198" "22.807647" "26.562865" "20.863743" ...
..- attr(*, "label")= chr "BMI"
$ HE_DM : 'labelled' chr "2.000000" "3.000000" "1.000000" "1.000000" ...
..- attr(*, "label")= chr "With diagnosis(over 19 year-old)"
$ LQ4_07 : 'labelled' chr "8.000000" "8.000000" "8.000000" "8.000000" ...
..- attr(*, "label")= chr "Barries for physical activity - diabetes"
$ HE_DMfh1 : 'labelled' chr "0.000000" "0.000000" "9.000000" "1.000000" ...
..- attr(*, "label")= chr "Father with diagnosis"
$ HE_DMfh2 : 'labelled' chr "1.000000" "0.000000" "9.000000" "0.000000" ...
..- attr(*, "label")= chr "Mother with diagnosis"
$ HE_DMfh3 : 'labelled' chr "0.000000" "0.000000" "9.000000" "0.000000" ...
..- attr(*, "label")= chr "Sibling with diagnosis"
$ HE_glu : 'labelled' chr "124.000000" "141.000000" "92.000000" "88.000000" ...
..- attr(*, "label")= chr "Diabetes indicator - glucose level"
$ BE5_1 : 'labelled' chr "1.000000" "1.000000" "1.000000" "1.000000" ...
..- attr(*, "label")= chr "Muscle training frequency"
$ LQ4_04 : 'labelled' chr "8.000000" "8.000000" "8.000000" "8.000000" ...
..- attr(*, "label")= chr "Barriers for physical activity - Have heart disease"
$ DF2_dg : 'labelled' chr "8.000000" "8.000000" "8.000000" "8.000000" ...
..- attr(*, "label")= chr "Diagnosed with depression"
$ HE_IHDfh1: 'labelled' chr "0.000000" "0.000000" "9.000000" "0.000000" ...
..- attr(*, "label")= chr "Diagnosed with Ischaemic heart disease"
$ HE_HP : 'labelled' chr "3.000000" "3.000000" "2.000000" "1.000000" ...
..- attr(*, "label")= chr "Hypertension Status (three levels)"
freq_table <- function (y) {
d <- select (y) %>% group_by (y) %>% summarise (n = n ()) %>% mutate (freq = n / sum (n))
}
lapply(diabetes$marri_1, freq_table)
The select function is at the beginning of the pipe and needs at least two arguments, you can add the name of the dataframe to the argument function
Also, because y is stored in a variable you have to unquote it when using the dplyr verbs by adding !! before it.
library(tidyverse)
# add df as an argument and add it before the select
freq_table <- function (df,y) {
d <- df %>% select (!! y) %>% group_by (!! y) %>% summarise (n = n ()) %>% mutate (freq = n / sum (n))
}
freq_table(diabetes,"marri_1")
Or in a more simple way you can do
tab <- table(diabetes$marri_1)
tab <- as.data.frame(tab)
names(tab) <- c("marri_1","n")
tab$freq <- tab$n /sum(tab$n)
Is this what you were looking for ?

R kohonen - Is the input data scaled and centred automatically?

I have been following an online example for R Kohonen self-organising maps (SOM) which suggested that the data should be centred and scaled before computing the SOM.
However, I've noticed the object created seems to have attributes for centre and scale, in which case am I really applying a redundant step by centring and scaling first? Example script below
# Load package
require(kohonen)
# Set data
data(iris)
# Scale and centre
dt <- scale(iris[, 1:4],center=TRUE)
# Prepare SOM
set.seed(590507)
som1 <- som(dt,
somgrid(6,6, "hexagonal"),
rlen=500,
keep.data=TRUE)
str(som1)
The output from the last line of the script is:
List of 13
$ data :List of 1
..$ : num [1:150, 1:4] -0.898 -1.139 -1.381 -1.501 -1.018 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length"
"Petal.Width"
.. ..- attr(*, "scaled:center")= Named num [1:4] 5.84 3.06 3.76 1.2
.. .. ..- attr(*, "names")= chr [1:4] "Sepal.Length" "Sepal.Width"
"Petal.Length" "Petal.Width"
.. ..- attr(*, "scaled:scale")= Named num [1:4] 0.828 0.436 1.765 0.762
.. .. ..- attr(*, "names")= chr [1:4] "Sepal.Length" "Sepal.Width"
"Petal.Length" "Petal.Width"
$ unit.classif : num [1:150] 3 5 5 5 4 2 4 4 6 5 ...
$ distances : num [1:150] 0.0426 0.0663 0.0768 0.0744 0.1346 ...
$ grid :List of 6
..$ pts : num [1:36, 1:2] 1.5 2.5 3.5 4.5 5.5 6.5 1 2 3 4 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : NULL
.. .. ..$ : chr [1:2] "x" "y"
..$ xdim : num 6
..$ ydim : num 6
..$ topo : chr "hexagonal"
..$ neighbourhood.fct: Factor w/ 2 levels "bubble","gaussian": 1
..$ toroidal : logi FALSE
..- attr(*, "class")= chr "somgrid"
$ codes :List of 1
..$ : num [1:36, 1:4] -0.376 -0.683 -0.734 -1.158 -1.231 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:36] "V1" "V2" "V3" "V4" ...
.. .. ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length"
"Petal.Width"
$ changes : num [1:500, 1] 0.0445 0.0413 0.0347 0.0373 0.0337 ...
$ alpha : num [1:2] 0.05 0.01
$ radius : Named num [1:2] 3.61 0
..- attr(*, "names")= chr [1:2] "66.66667%" ""
$ user.weights : num 1
$ distance.weights: num 1
$ whatmap : int 1
$ maxNA.fraction : int 0
$ dist.fcts : chr "sumofsquares"
- attr(*, "class")= chr "kohonen"
Note notice that in lines 7 and 10 of the output there are references to centre and scale. I would appreciate an explanation as to the process here.
Your step with scaling is not redundant because in source code there are no scaling, and attributes, that you see in 7 and 10 are attributes from train dataset.
To check this, just run and compare results of this chunk of code:
# Load package
require(kohonen)
# Set data
data(iris)
# Scale and centre
dt <- scale(iris[, 1:4],center=TRUE)
#compare train datasets
str(dt)
str(as.matrix(iris[, 1:4]))
# Prepare SOM
set.seed(590507)
som1 <- kohonen::som(dt,
kohonen::somgrid(6,6, "hexagonal"),
rlen=500,
keep.data=TRUE)
#without scaling
som2 <- kohonen::som(as.matrix(iris[, 1:4]),
kohonen::somgrid(6,6, "hexagonal"),
rlen=500,
keep.data=TRUE)
#compare results of som function
str(som1)
str(som2)

R haven: missing labels and label names when reading spss file

I'm using the haven package for R to read an spss file with user_na=TRUE. The file has many string variables with value labels. In R only the first of the string variables (SizeofH1) has the correct value labels assigned to it as attribute.
Unfortunately I cannot not even provide a snippet of this data to make this fully reproducible but here is a screenshot of what I can see in PSPP
and what str() in R returns...
$ SizeofH1:Class 'labelled' atomic [1:280109] 3 3 3 3 ...
..- attr(*, "label")= chr "Size of Household ab 2002"
..- attr(*, "format.spss")= chr "A30"
..- attr(*, "labels")= Named chr [1:9] "1" "2" "3" "4" ...
..- attr(*, "names")= chr [1:9] "4 Persons" "2 Persons" "1 Person 50 years plus" "3 Persons" ...
$ PROMOTIO: atomic 40 1 40 40 ...
..- attr(*, "label")= chr "PROMOTION"
..- attr(*, "format.spss")= chr "A30"
$ inFMCGfr: atomic 1 1 1 1 ...
..- attr(*, "label")= chr "in FMCG from2011"
..- attr(*, "format.spss")= chr "A30"
$ TRADESEG: atomic 1 1 1 1 ...
..- attr(*, "label")= chr "TRADE SEGMENT"
..- attr(*, "format.spss")= chr "A30"
$ ORGANISA: atomic 111 111 111 111 ...
..- attr(*, "label")= chr "ORGANISATION"
..- attr(*, "format.spss")= chr "A30"
$ NAME : atomic 9 9 9 9 ...
..- attr(*, "label")= chr "NAME"
..- attr(*, "format.spss")= chr "A30"
I hope someone can point me to any possible reason that causes this behavior.
The "semantics" vignette has some useful information on this topic.
library(haven)
vignette('semantics')
There are a couple of options to get value labels. I think a good one is the example demonstrated below, using the map function from the purrr package (but could be done with lapply instead, too)
# Get data from spss file
df <- read_sav(path_to_file)
# get value labels
df <- map_df(.x = df, .f = function(x) {
if (class(x) == 'labelled') as_factor(x)
else x})
# get column names
colnames(df) <- map(.x = spss_file, .f = function(x) {attr(x, 'label')})
The best is to save your spss file as CSV and then read it in R. I've faced this before and some strings didn't read correctly- Generally SPSS is not very smart when it comes to string variables that this could contribute to the problem.

alternative to `str()` in R

Perhaps it is just me, but I have always found str unsatisfactory. It is frequently too verbose, yet not very informative in many occasions.
I actually really like the description of the function (?str):
Compactly display the internal structure of an R object
and this bit in particular
Ideally, only one line for each ‘basic’ structure is displayed.
Only that, in many cases, the default str implementation simply does not do justice to such description.
Ok, let's say it works partially good for data.frames.
library(ggplot2)
str(mpg)
> str(mpg)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 234 obs. of 11 variables:
$ manufacturer: chr "audi" "audi" "audi" "audi" ...
$ model : chr "a4" "a4" "a4" "a4" ...
$ displ : num 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
$ year : int 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
$ cyl : int 4 4 4 4 6 6 6 4 4 4 ...
$ trans : chr "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
$ drv : chr "f" "f" "f" "f" ...
$ cty : int 18 21 20 21 16 18 18 18 16 20 ...
$ hwy : int 29 29 31 30 26 26 27 26 25 28 ...
$ fl : chr "p" "p" "p" "p" ...
$ class : chr "compact" "compact" "compact" "compact" ...
Yet, for a data.frame it's not as informative as I would like. In addition to class, it would be very useful that it shows number of NA values, and number of unique values, for example.
But for other objects, it quickly becomes unmanageable. For example:
gp <- ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point()
str(gp)
> str(gp)
List of 9
$ data :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 234 obs. of 11 variables:
..$ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
..$ model : chr [1:234] "a4" "a4" "a4" "a4" ...
..$ displ : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
..$ year : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
..$ cyl : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
..$ trans : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
..$ drv : chr [1:234] "f" "f" "f" "f" ...
..$ cty : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
..$ hwy : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
..$ fl : chr [1:234] "p" "p" "p" "p" ...
..$ class : chr [1:234] "compact" "compact" "compact" "compact" ...
$ layers :List of 1
..$ :Classes 'LayerInstance', 'Layer', 'ggproto' <ggproto object: Class LayerInstance, Layer>
aes_params: list
compute_aesthetics: function
compute_geom_1: function
compute_geom_2: function
compute_position: function
compute_statistic: function
data: waiver
draw_geom: function
geom: <ggproto object: Class GeomPoint, Geom>
aesthetics: function
default_aes: uneval
draw_group: function
draw_key: function
draw_layer: function
draw_panel: function
extra_params: na.rm
handle_na: function
non_missing_aes: size shape
parameters: function
required_aes: x y
setup_data: function
use_defaults: function
super: <ggproto object: Class Geom>
geom_params: list
inherit.aes: TRUE
layer_data: function
map_statistic: function
mapping: NULL
position: <ggproto object: Class PositionIdentity, Position>
compute_layer: function
compute_panel: function
required_aes:
setup_data: function
setup_params: function
super: <ggproto object: Class Position>
print: function
show.legend: NA
stat: <ggproto object: Class StatIdentity, Stat>
compute_group: function
compute_layer: function
compute_panel: function
default_aes: uneval
extra_params: na.rm
non_missing_aes:
parameters: function
required_aes:
retransform: TRUE
setup_data: function
setup_params: function
super: <ggproto object: Class Stat>
stat_params: list
subset: NULL
super: <ggproto object: Class Layer>
$ scales :Classes 'ScalesList', 'ggproto' <ggproto object: Class ScalesList>
add: function
clone: function
find: function
get_scales: function
has_scale: function
input: function
n: function
non_position_scales: function
scales: list
super: <ggproto object: Class ScalesList>
$ mapping :List of 2
..$ x: symbol displ
..$ y: symbol hwy
$ theme : list()
$ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto' <ggproto object: Class CoordCartesian, Coord>
aspect: function
distance: function
expand: TRUE
is_linear: function
labels: function
limits: list
range: function
render_axis_h: function
render_axis_v: function
render_bg: function
render_fg: function
train: function
transform: function
super: <ggproto object: Class CoordCartesian, Coord>
$ facet :List of 1
..$ shrink: logi TRUE
..- attr(*, "class")= chr [1:2] "null" "facet"
$ plot_env :<environment: R_GlobalEnv>
$ labels :List of 2
..$ x: chr "displ"
..$ y: chr "hwy"
- attr(*, "class")= chr [1:2] "gg" "ggplot"
Whaaattttt???, what happened to "Compactly display". That's not compact!
And it can be worse, crazy scary, for example, for S4 objects. If you want try this:
library(rworldmap)
newmap <- getMap(resolution = "coarse")
str(newmap)
I do not post the output here because it is too much. It does not even fit in the console buffer!
How can you possibly understand the internal structure of the object with such a NON-compact display? It's just too many details and you easily get lost. Or at least I do.
Well, all right. Before someone tells me, hey checkout ?str and tweak the arguments, that's what I did. Of course it can get better, but I am still kind of disappointed with str.
The best solution I've got is to create a function that do this
if(isS4(obj)){
str(obj, max.level = 2, give.attr = FALSE, give.head = FALSE)
} else {
str(obj, max.level = 1, give.attr = FALSE, give.head = FALSE)
}
This displays compactly the top level structures of the object. The output for the sp object above (S4 object) becomes much more insightful
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
..# data :'data.frame': 243 obs. of 49 variables:
..# polygons :List of 243
.. .. [list output truncated]
..# plotOrder :7 135 28 167 31 23 9 66 84 5 ...
..# bbox :-180 -90 180 83.6
..# proj4string:Formal class 'CRS' [package "sp"] with 1 slot
So now you can see there are 5 top level structures, and you can investigate them further individually.
Similar for the ggplot object above, now you can see
List of 9
$ data :Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 234 obs. of 11 variables:
$ layers :List of 1
$ scales :Classes 'ScalesList', 'ggproto'
$ mapping :List of 2
$ theme : list()
$ coordinates:Classes 'CoordCartesian', 'Coord', 'ggproto'
$ facet :List of 1
$ plot_env :
$ labels :List of 2
Although this is much better, I still feel it could be much more insightful. So, perhaps someone has felt the same way and created a nice function that is more informative and still compactly displays the information. Anyone?
In such situation I use glimpse from the tibble package which is less verbose and briefly descriptive of the data structure.
library(tibble)
glimpse(gp)
There is the lobstr package by Hadley. Besides several other more or less helpful functions it includes lobstr::tree() which tries to be more predictable, compact and overall more helpful than str().
An important difference between the two is that str() is an S3 generic whereas lobstr::tree() is not. That means package developers can and will include their own methods for str() which can substantially improve the usefulness of str(). But it also means that str() output can be very inconsistent.
For comparison, here is a display of the structure of a simple lm() with both functions. lobstr::tree() also prints a colorized output, which improves legibility further, but you obviously can't see the colors here on SO. Note in particular the much more concise and useful parts of the formula and the data frame items:
m <- lm(mpg~cyl, mtcars)
lobstr::tree(m)
#> S3<lm>
#> ├─coefficients<dbl [2]>: 37.8845764854614, -2.87579013906448
#> ├─residuals<dbl [32]>: 0.370164348925359, 0.370164348925418, -3.58141592920354, 0.770164348925411, 3.82174462705436, -2.52983565107459, -0.578255372945636, -1.98141592920354, -3.58141592920354, -1.42983565107459, ...
#> ├─effects<dbl [32]>: -113.649737406208, -28.5956806590543, -3.70425398161014, 0.709596949580206, 3.82344788077055, -2.59040305041979, -0.576552119229446, -2.10425398161014, -3.70425398161014, -1.49040305041979, ...
#> ├─rank: 2
#> ├─fitted.values<dbl [32]>: 20.6298356510746, 20.6298356510746, 26.3814159292035, 20.6298356510746, 14.8782553729456, 20.6298356510746, 14.8782553729456, 26.3814159292035, 26.3814159292035, 20.6298356510746, ...
#> ├─assign<int [2]>: 0, 1
#> ├─qr: S3<qr>
#> │ ├─qr<dbl [64]>: -5.65685424949238, 0.176776695296637, 0.176776695296637, 0.176776695296637, 0.176776695296637, 0.176776695296637, 0.176776695296637, 0.176776695296637, 0.176776695296637, 0.176776695296637, ...
#> │ ├─qraux<dbl [2]>: 1.17677669529664, 1.01602374277435
#> │ ├─pivot<int [2]>: 1, 2
#> │ ├─tol: 1e-07
#> │ └─rank: 2
#> ├─df.residual: 30
#> ├─xlevels: <list>
#> ├─call: <language> lm(formula = mpg ~ cyl, data = mtcars)
#> ├─terms: S3<terms/formula> mpg ~ cyl
#> └─model: S3<data.frame>
#> ├─mpg<dbl [32]>: 21, 21, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, ...
#> └─cyl<dbl [32]>: 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, ...
str(m)
#> List of 12
#> $ coefficients : Named num [1:2] 37.88 -2.88
#> ..- attr(*, "names")= chr [1:2] "(Intercept)" "cyl"
#> $ residuals : Named num [1:32] 0.37 0.37 -3.58 0.77 3.82 ...
#> ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
#> $ effects : Named num [1:32] -113.65 -28.6 -3.7 0.71 3.82 ...
#> ..- attr(*, "names")= chr [1:32] "(Intercept)" "cyl" "" "" ...
#> $ rank : int 2
#> $ fitted.values: Named num [1:32] 20.6 20.6 26.4 20.6 14.9 ...
#> ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
#> $ assign : int [1:2] 0 1
#> $ qr :List of 5
#> ..$ qr : num [1:32, 1:2] -5.657 0.177 0.177 0.177 0.177 ...
#> .. ..- attr(*, "dimnames")=List of 2
#> .. .. ..$ : chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
#> .. .. ..$ : chr [1:2] "(Intercept)" "cyl"
#> .. ..- attr(*, "assign")= int [1:2] 0 1
#> ..$ qraux: num [1:2] 1.18 1.02
#> ..$ pivot: int [1:2] 1 2
#> ..$ tol : num 1e-07
#> ..$ rank : int 2
#> ..- attr(*, "class")= chr "qr"
#> $ df.residual : int 30
#> $ xlevels : Named list()
#> $ call : language lm(formula = mpg ~ cyl, data = mtcars)
#> $ terms :Classes 'terms', 'formula' language mpg ~ cyl
#> .. ..- attr(*, "variables")= language list(mpg, cyl)
#> .. ..- attr(*, "factors")= int [1:2, 1] 0 1
#> .. .. ..- attr(*, "dimnames")=List of 2
#> .. .. .. ..$ : chr [1:2] "mpg" "cyl"
#> .. .. .. ..$ : chr "cyl"
#> .. ..- attr(*, "term.labels")= chr "cyl"
#> .. ..- attr(*, "order")= int 1
#> .. ..- attr(*, "intercept")= int 1
#> .. ..- attr(*, "response")= int 1
#> .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
#> .. ..- attr(*, "predvars")= language list(mpg, cyl)
#> .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
#> .. .. ..- attr(*, "names")= chr [1:2] "mpg" "cyl"
#> $ model :'data.frame': 32 obs. of 2 variables:
#> ..$ mpg: num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
#> ..$ cyl: num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
#> ..- attr(*, "terms")=Classes 'terms', 'formula' language mpg ~ cyl
#> .. .. ..- attr(*, "variables")= language list(mpg, cyl)
#> .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
#> .. .. .. ..- attr(*, "dimnames")=List of 2
#> .. .. .. .. ..$ : chr [1:2] "mpg" "cyl"
#> .. .. .. .. ..$ : chr "cyl"
#> .. .. ..- attr(*, "term.labels")= chr "cyl"
#> .. .. ..- attr(*, "order")= int 1
#> .. .. ..- attr(*, "intercept")= int 1
#> .. .. ..- attr(*, "response")= int 1
#> .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
#> .. .. ..- attr(*, "predvars")= language list(mpg, cyl)
#> .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
#> .. .. .. ..- attr(*, "names")= chr [1:2] "mpg" "cyl"
#> - attr(*, "class")= chr "lm"
Created on 2022-11-23 with reprex v2.0.2

Extracting component from all objects within an indexed list

I have an indexed list containing several objects each of which contains 3 matrices ($tab, $nobs and $other). There are hundred such objects in the list. The objective is to access only $tab matrix and transpose it from each of the objects.
genfreqT <- lapply(genfreq[[1:100]]$tab, function(x) t(x))
This does not seem to work.
Here is how the genfreq object is structured. This was created with R package adegenet.
> str(genfreq[[1]])
List of 3
$ tab : num [1:30, 1:1974] 0.6 0.5 0.325 0.675 0.6 0.5 0.5 0.375 0.55 0.475 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : Named chr [1:30] "1" "2" "3" "4" ...
.. .. ..- attr(*, "names")= chr [1:30] "01" "02" "03" "04" ...
.. ..$ : chr [1:1974] "L0001.1" "L0001.2" "L0002.1" "L0002.2" ...
$ nobs: num [1:30, 1:1000] 40 40 40 40 40 40 40 40 40 40 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : Named chr [1:30] "1" "2" "3" "4" ...
.. .. ..- attr(*, "names")= chr [1:30] "01" "02" "03" "04" ...
.. ..$ : Named chr [1:1000] "L0001" "L0002" "L0003" "L0004" ...
.. .. ..- attr(*, "names")= chr [1:1000] "L0001" "L0002" "L0003" "L0004" ...
$ call: language makefreq(x = x, truenames = TRUE)
genfreqT <-lapply(lapply(genfreq, "[[", "tab"),function(x) t(x))
The package developer for 'Adegenet' provided this solution:
> genfreqT <- lapply(genfreq, function(e) t(e$tab))
> summary(genfreqT)
Length Class Mode
data1.str 59220 -none- numeric
data2.str 59220 -none- numeric
data3.str 59220 -none- numeric

Resources