如何从另一个data.table重命名R数据表中的级别? [英] How to rename levels in an R data.table from another data.table?

查看:320
本文介绍了如何从另一个data.table重命名R数据表中的级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个data.tables, dt 是一个长整型列级别在范围1 ... 5,另一个data.tablelabels包含如下简单形式的标签:

  labels< data.table(V1 = 1:5,V2 = c(Very Low,Low,Median,High,Very High))
#V1 V2
#1 :1很低
#2:2低
#3:3中位数
#4:4高
#5:5非常高

实际的 dt 是相当大的,但是为了再现性,一个简单的在实际的DT级别不是常规的):

  dt < times = 10))



如何替换 dt <



我可以在手动循环中执行此操作(code> ugly!),或者我可以通过添加另一个列,如下所示:

  dt [,tmp:= labels $ V2 [dt $ level]] 

,然后删除栏 $ c>并重命名 tmp



有很好的data.table方法吗?

解决方案

假设您的数据集是这样生成的:

 code> dt<  -  data.table(levels = rep(1:5,times = 10))
labels< - data.table(V1 = 1:5,V2 = c低,低,中值,高,非常高))

然后,您可以使用 factor 函数重新标记 dt 的级别:

  dt [,level:= as.character(factor(level,labels = labels $ V2))] 
pre>

如果你不介意类型 factor ,你可以跳过 as.character 并且只执行:

  dt [,level:= factor = labels $ V2)] 


I have two data.tables, dt is a long one with an integer column levels in the range 1...5, and another data.table "labels" containing labels in a simple form like this:

labels <- data.table(V1=1:5, V2=c("Very Low", "Low", "Median", "High", "Very High"))
#    V1       V2
# 1:  1       Very Low
# 2:  2       Low
# 3:  3       Median
# 4:  4       High
# 5:  5       Very High

The actual dt is rather large, but for reproducibility a simple one will do (though in real DT levels are not that regular):

dt <- data.table(levels=rep(1:5, times=10))

How I could replace levels column in dt with character labels from labels in one go?

I could do this in manual loop (ugly!), or I could do this by adding another column, like this:

dt[, tmp := labels$V2[dt$level] ]

and then dropping column level and renaming tmp.

Is there a good data.table way to do so?

解决方案

Suppose that your datasets are generated like this:

 dt <- data.table(levels=rep(1:5, times=10))
 labels <- data.table(V1=1:5, V2=c("Very Low", "Low", "Median", "High", "Very High"))

Then you can "relabel" the levels of dt using the factor function:

dt[, level := as.character(factor(level, labels = labels$V2))]

If you don't mind level being of type factor, you can skip the as.character and just do:

dt[, level := factor(level, labels = labels$V2)]

这篇关于如何从另一个data.table重命名R数据表中的级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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