简体   繁体   English

如何通过粘贴为object分配名称

[英]How to assign a name to an object by pasting

Consider the data frame dat :考虑数据框dat

dat <- data.frame(Loc = c("NY","MA"),
                  ID = c(1:2),
                  Sex = c("M","F")
                  )

Pretending there were a lot more data to go with this, and multiple observations for each ID , I want to fit a model and save it in an object called NYM1mod , which to me would stand for "model for New York Male number 1" I can use: paste(dat[1,1], dat[1,3], dat[1,2],"mod", sep="") to output what I want: [1] "NYM1mod" But how can I do something like this and make the output the name of a new object?假装 go 有更多数据,并且每个ID有多个观察结果,我想拟合一个 model 并将其保存在一个名为NYM1mod的 object 中,对我来说它代表“纽约男一号模特”我可以使用: paste(dat[1,1], dat[1,3], dat[1,2],"mod", sep="") to output what I want: [1] "NYM1mod"但如何才能我做这样的事情并使 output 成为新的 object 的名称? For example, why can you not assign the number 3 to the output "NYM1mod" like this?: paste(dat[1,1], dat[1,3], dat[1,2],"mod", sep="") <- 3例如,为什么不能像这样将数字 3 分配给 output "NYM1mod"?: paste(dat[1,1], dat[1,3], dat[1,2],"mod", sep="") <- 3

As commented, consider using a list instead of many separate objects.如评论所述,考虑使用列表而不是许多单独的对象。 Specifically, generalize your modeling process in a defined function. Iterate it with lapply to return a list of objects.具体来说,在定义的 function 中概括您的建模过程。使用lapply对其进行迭代以返回对象列表。 Then rename with setNames to return a single, named list of objects, indexable for all.然后用setNames重命名以返回一个单一的、命名的对象列表,所有对象都可以索引。 some, or one item without flooding global environment with many, separate objects:一些或一个项目,而不会用许多单独的对象淹没全局环境:

proc_model <- function(...) {
    # MODELING PROCESS 
    ...
    return(model)
}

model_list <- setNames(lapply(sequence_pass_as_params, proc_model),
                       paste0(dat$Loc, dat$Sex, dat$ID, "mod"))

# ALL MODELS
model_list 

# SELECT MODELS
model_list$NYM1mod    
model_list$MAF2mod

In fact, if I understand your needs consider by (another sibling to apply family as object-oriented wrapper to tapply ) to pass subset data frames of Loc , ID , and Sex into your modeling function. Wrap process with tryCatch for subsets that do not exist and/or yields modeling errors.事实上,如果我了解您的需求,请考虑by另一个将 family 作为面向对象的包装器应用tapply的兄弟)将LocIDSex的子集数据帧传递到您的建模中 function。使用tryCatch包装进程不存在和/或产生建模错误。

# RECEIVE DATA FRAME AS PARAMETER
proc_model <- function(df) {
    tryCatch({
        # MODELING PROCESS 
        model <- lm(..., data = df)
        }, error = function(e) return(NULL)
    )
}

# BUILD MODEL LIST BY SUBSETS
model_list <- by(dat, dat[,c("Loc", "Sex", "ID"), proc_model)

# RENAME ELEMENTS IN LIST WITH UNIQUE NAMES
model_list <- setNames(model_list, unique(paste0(dat$Loc, dat$Sex, dat$ID, "mod")))

# REMOVE EMPTY MODELS  (I.E., ITEMS THAT RETURN NULL)
model_list <- Filter(model_list, LENGTH)

# ALL MODELS
model_list 

# SELECT MODELS
model_list$NYM1mod    
model_list$MAF2mod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM