7

我有六个人的列表,每个人包含 25 个数字。数据通过 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"))

str()数据看起来像这样。

> 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 ...

我正在尝试使用get()paste()返回 for() 循环中每个人的数字。

我可以在循环之外直接使用下面的代码来执行此操作。

data$AAA

或者

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

但是,我需要在循环中调用这些数字。我首先创建了一个 Name 对象为

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

并且可以正确返回个人使用的姓名。

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

但是,当在 get() 中包含相同的代码时,不会返回项目,并且会导致错误,如下所示。

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

虽然data$AAA是列表中的一个项目而不是一个对象,但为什么它在运行时返回一个值,但在使用 andget()运行paste()时却找不到?get()paste()

有没有更好的方法从列表中提取项目?

在 for() 循环中,我需要为每个单独的 AAA:FFF 提取数字并重新采样它们以作为引导带。

感谢您的任何建议。

4

1 回答 1

11

该函数get可用于返回对象。在您的情况下,data是一个对象,但data$AAA不返回一个对象,而是一个列表元素。

要访问循环中的列表元素,您可以使用名称向量Name或简单的整数与函数一起使用[[

data[[Name[1]]]

data[[1]]

此外,如果要在循环中使用所有列表元素,则不需要列表元素的名称。这可以通过例如 来完成lapply

lapply(data, sample)

上面的命令是一种简单的方法来打乱列表元素中值的顺序。

于 2013-11-12T20:56:08.680 回答