Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract items from a list using get() and paste() in R

Tags:

r

I have a list of six indviduals, each containing 25 numbers. The data is here through dput()

data <- structure(list(AAA = structure(c(0.539032790443548, 0.536888048404759, 
0.519687575144773, 0.540104624777809, 0.57386075783306, 0.539805870321112, 
0.538733934351732, 0.530445942604962, 0.521841030809798, 0.541176088266961, 
0.56539960323135, 0.551570726188792, 0.557196928234619, 0.517533387708244, 
0.567518667655521, 0.532295137298835, 0.542247171136739, 0.541948628127994, 
0.533668399132029, 0.546527501435108, 0.529071565146478, 0.575969506937413, 
0.556132191844378, 0.572805370086458, 0.525845566212544), .Dim = c(25L, 
1L)), BBB = structure(c(0.499216657615325, 0.537490952761078, 
0.43385212802169, 0.582152772934407, 0.495717756862187, 0.535025311378272, 
0.43385212802169, 0.607585618769066, 0.51961831826224, 0.477799201847025, 
0.593737672553431, 0.448083953208316, 0.564607973025657, 0.381933810565836, 
0.582152772934407, 0.555267755972877, 0.454004260019714, 0.596126995098342, 
0.469366643373233, 0.489741406245514, 0.39565274890788, 0.523110920308712, 
0.517142992101799, 0.39565274890788, 0.578744364954426), .Dim = c(25L, 
1L)), CCC = structure(c(0.517638997604126, 0.514052704141984, 
0.485341292016175, 0.524805767901646, 0.435170284293271, 0.485341292016175, 
0.481755279544946, 0.506876143514677, 0.421108263752241, 0.488653944275884, 
0.383154152708813, 0.578031931495085, 0.477896667937187, 0.539105776278097, 
0.520948955546837, 0.474589257707777, 0.499421739935859, 0.396817548905349, 
0.445786979965935, 0.492242533218636, 0.389964263092625, 0.56042847462084, 
0.503286615216397, 0.431644065154171, 0.438973968229145), .Dim = c(25L, 
1L)), DDD = structure(c(0.484888569519603, 0.442688918297825, 
0.518331393929951, 0.512548610084284, 0.482961110202505, 0.457970991381686, 
0.573751865786352, 0.438884146815107, 0.461805048643506, 0.533723983278093, 
0.545228033583635, 0.473332558849223, 0.558587821448958, 0.549052625000964, 
0.442688918297825, 0.452229366063252, 0.543313706170222, 0.479107769328041, 
0.5680800051331, 0.512548610084284, 0.433190571208039, 0.562390236072908, 
0.482961110202505, 0.549052625000964, 0.566185334180834), .Dim = c(25L, 
1L)), EEE = structure(c(0.527642738158034, 0.655613752398633, 
0.659899891320058, 0.577684574059289, 0.468878305768695, 0.417245905647398, 
0.51814290965504, 0.605280452996461, 0.398852141429807, 0.421882717287325, 
0.483124544962739, 0.522894894437057, 0.527642738158034, 0.544877520452231, 
0.522894894437057, 0.398852141429807, 0.475330590515985, 0.521185738519326, 
0.540151934786979, 0.440560204368187, 0.651302139891474, 0.435872884121988, 
0.426533342315385, 0.642604752301341, 0.525935170556392), .Dim = c(25L, 
1L)), FFF = structure(c(0.535178732002055, 0.590708080450234, 
0.397361678539085, 0.415528903431535, 0.553856005124287, 0.544533027664449, 
0.538299910477083, 0.394359689796758, 0.538299910477083, 0.510130509315003, 
0.590708080450234, 0.532054795832899, 0.560050751685107, 0.52892834473642, 
0.468102832205161, 0.530783113472337, 0.437014509571061, 0.566226849219331, 
0.502578884653375, 0.587669810559495, 0.409444994567033, 0.458739104066398, 
0.474358234263268, 0.599780569192313, 0.590708080450234), .Dim = c(25L, 
1L))), .Names = c("AAA", "BBB", "CCC", "DDD", "EEE", "FFF"))

The str() of data look like this.

> str(data)
List of 6
 $ AAA: num [1:25, 1] 0.539 0.537 0.52 0.54 0.574 ...
 $ BBB: num [1:25, 1] 0.499 0.537 0.434 0.582 0.496 ...
 $ CCC: num [1:25, 1] 0.518 0.514 0.485 0.525 0.435 ...
 $ DDD: num [1:25, 1] 0.485 0.443 0.518 0.513 0.483 ...
 $ EEE: num [1:25, 1] 0.528 0.656 0.66 0.578 0.469 ...
 $ FFF: num [1:25, 1] 0.535 0.591 0.397 0.416 0.554 ...

I am trying to use get() and paste() to return the numbers of each individual within a for() loop.

I can do this directly with to code below outside of the loop.

data$AAA

or

> head(data$AAA)
          [,1]
[1,] 0.5390328
[2,] 0.5368880
[3,] 0.5196876
[4,] 0.5401046
[5,] 0.5738608
[6,] 0.5398059

However, I need to call these numbers within a loop. I first created a Name object as

Name <- c("AAA","BBB", "CCC", "DDD", "EEE", "FFF")

And can correctly return the name of an individual using.

paste("data$", Name[1], sep="")
> paste("data$", Name[1], sep="")
[1] "data$AAA"

However, when the same code is wrapped in get() the items are not returned and an error results as seen below.

get(paste("data$", Name[1], sep=""))

> get(paste("data$", Name[1], sep=""))
Error in get(paste("data$", Name[1], sep = "")) : 
  object 'data$AAA' not found

While data$AAA is an item in a list rather than an object, why does it return a value when run outside of get() and paste() but cannot be found when run using get() and paste()?

Is there a better way to extract items from a list?

Within a for() loop I need to extract the numbers for each individual AAA:FFF and resample them for a boot strap.

Thanks for any suggestions.

like image 861
B. Davis Avatar asked Nov 12 '13 20:11

B. Davis


People also ask

How do I extract elements from a list in R?

The [[ operator can be used to extract single elements from a list. Here we extract the first element of the list. The [[ operator can also use named indices so that you don't have to remember the exact ordering of every element of the list. You can also use the $ operator to extract elements by name.

How do I get a list of values in R?

We can use the list() function to create a list. Another way to create a list is to use the c() function. The c() function coerces elements into the same type, so, if there is a list amongst the elements, then all elements are turned into components of a list.

How do I extract elements from a vector in R?

To extract (also known as indexing or subscripting) one or more values (more generally known as elements) from a vector we use the square bracket [ ] notation.

What does list () do in R?

The list() function in R is used to create a list of elements of different types. A list can contain numeric, string, or vector elements.


1 Answers

The function get can be used to return objects. In your case, data is an object, but data$AAA does not return an object, but a list element.

To access list elements in a loop you can either use your name vector Name or simply integers together with the function [[.

data[[Name[1]]]

data[[1]]

Furthermore, you don't need the names of the list elements if you want to use all list elements in a loop. This can be done with, e.g., lapply.

lapply(data, sample)

The above command is an easy way to shuffle the order of the values inside the list elements.

like image 68
Sven Hohenstein Avatar answered Sep 22 '22 03:09

Sven Hohenstein