简体   繁体   English

如何根据传递给 R 中函数的参数值重估因子中的元素

[英]How to revalue elements in factor based on values of arguments, passed to the function in R

Good morning,早上好,

Could you help me with the following problem?你能帮我解决以下问题吗? I have a function to which I pass certain values that are levels in a factor.我有一个函数,我向它传递某些作为因子水平的值。 I would like to revalue levels in factor, based on this values.我想根据这个值重新评估因子水平。

For example, if I want to revalue levels in factor my_factor ("cats" -> "animals", "pines" -> "trees"), I use: my_factor <- revalue(my_factor, c("cats"="animals", "pines"="trees")) .例如,如果我想重估因子my_factor ("cats" -> "animals", "pines" -> "trees") 中的my_factor <- revalue(my_factor, c("cats"="animals", "pines"="trees")) ,我使用: my_factor <- revalue(my_factor, c("cats"="animals", "pines"="trees")) But now I want to revalue levels, based on values of arguments, passed to the function :但是现在我想根据传递给函数的参数值重新评估级别

myFunction(..., member1 = "cats", member2 = "pines") {
my_factor <- revalue(my_factor, c(member1="animals", member2="trees"))
}

This fragment of code isn't working ( Error: The following from values were not present in x : member1, member2 ).此代码片段不起作用( Error: The following x values were not present in Error: The following values were not present in : member1, member2 )。

Please tell me how to do it correctly?请告诉我如何正确操作? Perhaps I need to use something other than revalue .也许我需要使用revalue以外的东西。

Good regards, Poecile问候, Poecile

You reference plyr , but that package is obsolete and its use is generally not recommended.您引用了plyr ,但该软件包已过时,通常不建议使用它。 I'm not going to attempt a solution in the dplyr manner, since I don't have a sufficient command of it's various levels of abstraction.我不打算以dplyr方式尝试解决方案,因为我对它的各种抽象级别没有足够的掌握。

The base function levels<- will do this cleanly.基本函数levels<-将干净利落地做到这一点。 When you do something like:当您执行以下操作时:

levels(fac)[some_index] <- "something"

You change the print value of that level without changing the underlying pattern of factor integers that carry the information.您可以更改该级别的打印值而不更改携带信息的因子整数的基本模式。 So use `levels(fac), once to get the current values of the levels to create a logical index to use inside "[" and again on the "outside" to do the reassignment:因此,使用`levels(fac),一次获取级别的当前值以创建一个逻辑索引以在“[”内部使用,并再次在“外部”上进行重新分配:

levels(fac)[ levels(fac) == "cats"] <- "animals"
levels(fac)[ levels(fac) == "pines"] <- "trees"

You are actually using two different functions: levels<- (on the outside) and levels (on the inside).您实际上使用了两个不同的函数: levels<- (在外部)和levels (在内部)。 To make this process into a function that can handle an arbitrary number of reassignments, you would want the reassignment pairs to be carried in a list of lists so you could iterate over the pairs.为了使这个过程成为可以处理任意数量的重新分配的函数,您需要将重新分配对包含在列表列表中,以便您可以迭代这些对。 Your current request is attempting to use a language-like expression such as "cats" = "animals" , but that would create a parameter named cats with a value of "animals"您当前的请求正在尝试使用类似语言的表达式,例如"cats" = "animals" ,但这会创建一个名为cats的参数,其值为"animals"

reval <- function(x)(fac, reassign) {
             lapply(reassign, function(fac, pair) {
                       levels(col)[levels(col)==pair[[1]]] <-pair[[2]]}
                                      return(fac) }

And you would call it like this:你会这样称呼它:

facname <- reval ( facname, list( list("curlev1", "newlev1"),
                                  list("curlev2", "newlev2")) )  )

If you have an example off the example naming you used "my_factor ("cats" -> "animals", "pines" -> "trees")" then test it with如果您在使用“my_factor ("cats" -> "animals", "pines" -> "trees")” 命名的示例中有一个示例,然后使用以下命令对其进行测试

 my_factor <- reval( my_factor, reassign = list (list("cats" , "animals"), 
                                           list("pines" , "trees") ) )

If it doesn't work then you should post R code to create an example that can be used for further development and testing.如果它不起作用,那么您应该发布 R 代码来创建一个可用于进一步开发和测试的示例。

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

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