如何从列表中选择元素作为名称范围以构建新列表?

huangapple go评论71阅读模式
英文:

How to select elements from a list as a range of names to build a new list?

问题

我有一个名为 spec_list 的列表。我想通过保留其中来自 AE:SUPPCM 的元素来获取一个新的列表。如何做到这一点?

我可以使用以下代码来实现:lst2 <- spec_list[c('AE', 'SUPPAE', 'CM', 'SUPPCM')],但我可以使用 AE:SUPPCM 来代替一个个列出名称吗?

str(spec_list, 1)
# 一个包含9个元素的列表
#  $ README          : chr "foobar"
#  $ SIGN_OFF        : logi TRUE
#  $ REVISION_HISTORY: logi NA
#  $ Study           : num 12345
#  $ Datasets        :List of 2
#  $ AE              : chr "yes"
#  $ SUPPAE          : int [1:10] 1 2 3 4 5 6 7 8 9 10
#  $ CM              : num 1.23e+08
#  $ SUPPCM          : int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...
英文:

I have a list of spec_list. I would like to get a new list by keeping elements from AE:SUPPCM. How can I do that?

I can do lst2 &lt;- spec_list[c(&#39;AE&#39;, &#39;SUPPAE&#39;, &#39;CM&#39;,&#39; SUPPCM&#39;)], but can I do this using AE:SUPPCM instead of listing the names one by one?

str(spec_list, 1)
# List of 9
#  $ README          : chr &quot;foobar&quot;
#  $ SIGN_OFF        : logi TRUE
#  $ REVISION_HISTORY: logi NA
#  $ Study           : num 12345
#  $ Datasets        :List of 2
#  $ AE              : chr &quot;yes&quot;
#  $ SUPPAE          : int [1:10] 1 2 3 4 5 6 7 8 9 10
#  $ CM              : num 1.23e+08
#  $ SUPPCM          : int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...

答案1

得分: 2

以下是代码的中文翻译:

这是一个可以帮助你实现这个功能的小函数 -

select_by_names <- function(data, x, y) {
  nm <- names(data)
  data[match(x, nm) : match(y, nm)]
}

我创建了自己的示例来展示这个函数的工作方式。

tmp <- head(mtcars, 2)
spec_list <- list(README = tmp, `SIGN-OFF` = tmp, `REVISION HISTORY` = tmp, 
                  AE = tmp, SUPPAE = tmp, CM = tmp, SUPPCM = tmp)

select_by_names(spec_list, 'AE', 'SUPPCM')

#$AE
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$SUPPAE
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$CM
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$SUPPCM
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

还有另一个示例 -

select_by_names(spec_list, 'SIGN-OFF', 'CM')

#$`SIGN-OFF`
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$`REVISION HISTORY`
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$AE
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$SUPPAE
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$CM
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

希望这对你有帮助!

英文:

Here's a small function that can help you do that -

select_by_names &lt;- function(data, x, y) {
  nm &lt;- names(data)
  data[match(x, nm) : match(y, nm)]
}

I have created my own example to show how this function works.

tmp &lt;- head(mtcars, 2)
spec_list &lt;- list(README = tmp, `SIGN-OFF` = tmp, `REVISION HISTORY` = tmp, 
                  AE = tmp, SUPPAE = tmp, CM = tmp, SUPPCM = tmp)

select_by_names(spec_list, &#39;AE&#39;, &#39;SUPPCM&#39;)

#$AE
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$SUPPAE
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$CM
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$SUPPCM
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

and another one -

select_by_names(spec_list, &#39;SIGN-OFF&#39;, &#39;CM&#39;)

#$`SIGN-OFF`
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$`REVISION HISTORY`
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$AE
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$SUPPAE
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

#$CM
#              mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
#Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

答案2

得分: 2

以下是翻译的部分:

这在你的列表是数据框架(data.frame)时很容易。

subset(head(mtcars), select=qsec:carb)
#                    qsec vs am gear carb
# Mazda RX4         16.46  0  1    4    4
# Mazda RX4 Wag     17.02  0  1    4    4
# Datsun 710        18.61  1  1    4    1
# Hornet 4 Drive    19.44  1  0    3    1
# Hornet Sportabout 17.02  0  0    3    2
# Valiant           20.22  1  0    3    1

它调用了subset.data.frame,我们可以稍微修改以使其适用于列表。

select <- function(x, select, ...) {
  if (missing(select)) {
    vars <- rep_len(TRUE, length(x))
  } else {
    nl <- as.list(seq_along(x))
    names(nl) <- names(x)
    vars <-  eval(substitute(select), nl, parent.frame())
  }
  x[vars]
}

用法

select(x=spec_list, select=AE:SUPPCM)

## 或者简写
select(spec_list, AE:SUPPCM)
# $AE
# [1] "yes"
# 
# $SUPPAE
# [1]  1  2  3  4  5  6  7  8  9 10
# 
# $CM
# [1] 123248756
# 
# $SUPPCM
# [,1] [,2] [,3] [,4]
# [1,]    1    4    7   10
# [2,]    2    5    8   11
# [3,]    3    6    9   12

也适用于数字,

select(spec_list, 6:9)

当然也适用于“data.frame”:

select(mtcars, qsec:carb)

数据:

spec_list <- list(README='foobar', SIGN_OFF=TRUE, REVISION_HISTORY=NA, 
                  Study=12345, Datasets=list(iris, mtcars), AE='yes', 
                  SUPPAE=1:10, CM=123248756, SUPPCM=matrix(1:12, 3, 4))
英文:

This is easy if your list was a data.frame.

subset(head(mtcars), select=qsec:carb)
#                    qsec vs am gear carb
# Mazda RX4         16.46  0  1    4    4
# Mazda RX4 Wag     17.02  0  1    4    4
# Datsun 710        18.61  1  1    4    1
# Hornet 4 Drive    19.44  1  0    3    1
# Hornet Sportabout 17.02  0  0    3    2
# Valiant           20.22  1  0    3    1

It dispatches to subset.data.frame, which we can slightly change to make it work for lists.

select &lt;- \(x, select, ...) {
  if (missing(select)) {
    vars &lt;- rep_len(TRUE, length(x))
  } else {
    nl &lt;- as.list(seq_along(x))
    names(nl) &lt;- names(x)
    vars &lt;-  eval(substitute(select), nl, parent.frame())
  }
  x[vars]
}

Usage

select(x=spec_list, select=AE:SUPPCM)

## or shortly
select(spec_list, AE:SUPPCM)
# $AE
# [1] &quot;yes&quot;
# 
# $SUPPAE
# [1]  1  2  3  4  5  6  7  8  9 10
# 
# $CM
# [1] 123248756
# 
# $SUPPCM
# [,1] [,2] [,3] [,4]
# [1,]    1    4    7   10
# [2,]    2    5    8   11
# [3,]    3    6    9   12

Also works with numbers,

select(spec_list, 6:9)

and of course with &quot;data.frame&quot;s.

select(mtcars, qsec:carb)

Data:

spec_list &lt;- list(README=&#39;foobar&#39;, SIGN_OFF=TRUE, REVISION_HISTORY=NA, 
                  Study=12345, Datasets=list(iris, mtcars), AE=&#39;yes&#39;, 
                  SUPPAE=1:10, CM=123248756, SUPPCM=matrix(1:12, 3, 4))

答案3

得分: 1

": "如果您愿意使用索引而不是数据帧名称,lst2会起作用。

英文:

':' Dos work if you are ok with using the index and not the df-names

lst2 &lt;- spec_list[6:9]

huangapple
  • 本文由 发表于 2023年3月4日 08:29:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定