构建一个命名列表,而不必两次输入每个对象的名称

构建一个命名列表,而不必两次输入每个对象的名称,第1张

在交互式使用中,我偶尔需要捆绑一套较大的 列表中的对象。获取元素保留的列表 他们原来的名字,我被迫写了类似的东西 list(Object1=Object1, Object2=Object2, ..... , Object25=Object25)

是否有一些简单的方法可以在列表中放置一组命名对象,以便他们“保留”其名称,而无需为每个命名对象nameXXX=nameXXX

cars <- mtcars[1:2,1:2]
vowels <- c("a","e","i","o","u")
consonants <- setdiff(letters, vowels)

## I'd like to get this result...
list(consonants=consonants, vowels=vowels, cars=cars)
## $consonants
##  [1] "b" "c" "d" "f" "g" "h" "j" "k" "l" "m" "n" "p" "q" "r" "s" "t" "v" "w" "x"
## [20] "y" "z"
##
## $vowels
## [1] "a" "e" "i" "o" "u"
##
## $cars
##               mpg cyl
## Mazda RX4      21   6
## Mazda RX4 Wag  21   6

## ... but by doing something more like
f(consonants, vowels, cars)

最佳答案:

3 个答案:

答案 0 :(得分:18)

您可以使用

获得相同的结构
mget(c("vowels", "consonants", "cars"))

但你必须引用不是超级性感的变量名。

答案 1 :(得分:10)

这是我最近使用的内容。

但是,如果有更简洁的东西(或基础R或一个体面的包装内置的东西)会很好,所以请随意添加其他/更好的答案。

/** 
 * An enum that is used within the Cache annotation.
 * 
 * @see org.eclipse.persistence.annotations.Cache
 * @author Guy Pelletier
 * @since Oracle TopLink 11.1.1.0.0 
 */ 
public enum CacheCoordinationType {
    /**
     * Sends a list of changed objects including data about the changes. 
     * This data is merged into the receiving cache.
     */
    SEND_OBJECT_CHANGES,

    /**
     * Sends a list of the identities of the objects that have changed. The 
     * receiving cache invalidates the objects (rather than changing any of the 
     * data)
     */
    INVALIDATE_CHANGED_OBJECTS,

    /**
     * Same as SEND_OBJECT_CHANGES except it also includes any newly created 
     * objects from the transaction.
     */
    SEND_NEW_OBJECTS_WITH_CHANGES,

    /**
     * Does no cache coordination.
     */
    NONE
}

答案 2 :(得分:1)

怎么样:

namedList  <-  function(...){
    out  <-  list(...)
    for(i in seq(length(out)))
        names(out)[i]  <-  as.character(sys.call()[[i 1]])
    out
}

foo = 1
bar = 'Hello world'
namedList(foo,bar)

#> $foo
#> [1] 1
#> 
#> $bar
#> [1] "Hello world"
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复