申请家庭内如何分配? [英] How to assign within apply family?

查看:15
本文介绍了申请家庭内如何分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含多个因子的 data.frame,我想为所有这些因子重命名因子级别.例如:

I have data.frame that contains several factors and i want to rename factor levels for all of these factors. E.g.:

mydf <- data.frame(col1 = as.factor(c("A","A",NA,NA)),col2 = as.factor(c("A",NA,NA,"A")))
mydf <- as.data.frame(lapply(mydf,addNA))

请注意,现实生活中的示例不仅仅是两列.因此,我想使用 apply 为所有这些列分配其他级别名称,就像在:

Note that the real life example has way more than just two columns. Hence I would like to use apply to assign other level names to all of these columns, just like in:

levels(mydf$col1) <- c("1","0") 

我尝试了以下方法,但没有用……

I tried the following but it did not work…

 apply(mydf,1,function(x) levels(x) <- c("1","0"))

我并不惊讶它不起作用,但我现在没有更好的想法.我应该使用 with 吗?

I am not really surprised it doesn't work but I have no better ideas right now. Should I use with maybe?

我意识到我在这里过于简单化了一个错误.我使用 addNA 来解释这样一个事实,即 NA 不应再作为 NA 处理.因此,我也想重新标记它们.这不适用于 Andrie 的建议并返回以下错误消息:

I realized I made a mistake here by oversimplifying things. I used addNA to account for the fact, that NAs should not handled as NAs anymore. Thus I also want to relabel them. This doesn't work with Andrie's suggestion and returns the following error message:

 labels = c("1",  : invalid labels; length 2 should be 1 or 1  

请注意,我更新了示例 df.

Note that I updated my example df.

推荐答案

您可以使用包 bitdata.table.这避免了复制整个数据集,而且既然你说你有很多列......

You can change levels by reference using setattr() from packages bit or data.table. This avoids copying the whole dataset, and since you said you have a lot of columns ...

require(bit)          # Either package
require(data.table)   #
setattr(mydf[[1]],"levels",c("1","0"))
setattr(mydf[[2]],"levels",c("1","0"))

这可以在一个非常快的简单 for 循环中完成.您有责任确保用相同长度的向量替换级别向量,否则该因子可能不再有效.并且,您必须使用此方法替换整个级别向量.data.table 中有一种内部方法可以通过引用替换特定的级别名称,但可能不需要走那么远.

That can be done in a simple for loop which is very fast. It is your responsibility to ensure that you replace the levels vector with a vector of the same length, otherwise the factor may no longer be valid. And, you have to replace the whole levels vector with this method. There is an internal way in data.table to replace particular level names by reference, but probably no need to go that far.

这篇关于申请家庭内如何分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆