Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 81

4.3.6.2 Naming Elements of Lists

Оглавление

While it is perfectly possible to address elements of lists by their number, it is sometimes more meaningful to use a specific name.

# Create the list: L <- list(“Approximation”, pi, 3.14, c) # Assign names to elements: names(L) <- c(“description”, “exact”, “approx”,“function”) print(L) ## $description ## [1] “Approximation” ## ## $exact ## [1] 3.141593 ## ## $approx ## [1] 3.14 ## ## $`function` ## function (…) .Primitive(“c”) # Addressing elements of the named list: print(paste(“The difference is”, L$exact - L$approx)) ## [1] “The difference is 0.00159265358979299” print(L[3]) ## $approx ## [1] 3.14 print(L$approx) ## [1] 3.14 # However, “function” was a reserved word, so we need to use # back-ticks in order to address the element: a <- L$`function`(2,3,pi,5) # to access the function c(…) print(a) ## [1] 2.000000 3.000000 3.141593 5.000000

The Big R-Book

Подняться наверх