How to assign within apply family?

狂风中的少年 提交于 2019-12-10 14:19:22

问题


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))

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"))

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

EDIT: 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  

Note that I updated my example df.


回答1:


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"))

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.



来源:https://stackoverflow.com/questions/9463980/how-to-assign-within-apply-family

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!