结合R中数据帧的因子级别

时间:2018-03-23 22:44:52

标签: r r-factor

我有factor类型的变量,有三个级别:Fatal injuryNon-fatal injuryP.D. only

     head(OttawaCollisions$Collision_Classification)
[1] P.D. only        Non-fatal injury P.D. only        P.D. only        P.D. only        P.D. only       
Levels: Fatal injury Non-fatal injury P.D. only

如何将“致命伤”和“非致命伤”合并到一个单一级别,以便将伤亡加重?

更好的是,我能不能以某种方式消除死亡事件?在这种情况下,我需要从数据框中删除致命的每个实例,而不仅仅是编码NA或其他东西。

2 个答案:

答案 0 :(得分:2)

数据:

x <- factor( rep( c('P.D. only', 'Non-fatal injury' , 'fatal injury'), 2) )
x
# [1] P.D. only        Non-fatal injury fatal injury     P.D. only       
# [5] Non-fatal injury fatal injury    
# Levels: fatal injury Non-fatal injury P.D. only

代码:您可以使用labels参数重命名该级别。忽略重复级别的警告。此处Non-fatal injuryfatal injuryFatalities合并在一起。最后,使用droplevels()函数删除重复的级别。

x <- factor( x = x, 
             levels = c('P.D. only', 'Non-fatal injury' , 'fatal injury'),
             labels = c('P.D. only', 'Fatalities', 'Fatalities'))
# [1] P.D. only  Fatalities Fatalities P.D. only  Fatalities Fatalities
# Levels: P.D. only Fatalities Fatalities

droplevels(x)
# [1] P.D. only  Fatalities Fatalities P.D. only  Fatalities Fatalities
# Levels: P.D. only Fatalities

编辑:根据您的数据框名称组合代码

OttawaCollisions$CollisionClass <- factor( x = OttawaCollisions$CollisionClass, 
                                           levels = c('P.D. only', 'Non-fatal injury' , 'fatal injury'),
                                           labels = c('P.D. only', 'Fatalities', 'Fatalities'))
OttawaCollisions$CollisionClass <- droplevels(OttawaCollisions$CollisionClass)

EDIT2: data.table解决方案。

library('data.table')
setDT(OttawaCollisions)
OttawaCollisions[ i = CollisionClass %in% c( "fatal injury", "Non-fatal injury"), 
                  j = CollisionClass := "Fatalities"]
OttawaCollisions[, CollisionClass := droplevels(CollisionClass) ]

EDIT3:另一个基础R解决方案。我更喜欢这个基本的R解决方案,而不是第一个解决方案(在labels中使用factor()),因为当数据中有更多级别时,它会让生活更轻松。

OttawaCollisions$CollisionClass <- as.character(OttawaCollisions$CollisionClass)
OttawaCollisions$CollisionClass <- factor( with(OttawaCollisions, 
                                                replace( CollisionClass, 
                                                         CollisionClass %in% c( "fatal injury", "Non-fatal injury"),
                                                         "Fatalities") ) )

答案 1 :(得分:1)

您也可以直接重新分配关卡:

> test_df <- tibble(x=as.factor(c('Fatal','Non-fatal','PD','Fatal','Non-fatal','PD')), y=1:6)
> test_df
# A tibble: 6 x 2
  x             y
  <fct>     <int>
1 Fatal         1
2 Non-fatal     2
3 PD            3
4 Fatal         4
5 Non-fatal     5
6 PD            6
> levels(test_df$x)
[1] "Fatal"     "Non-fatal" "PD"       

现在您知道了订单,请替换要组合的级别名称:

> levels(test_df$x) <- c("Fatal","Other","Other")
> test_df
# A tibble: 6 x 2
  x         y
  <fct> <int>
1 Fatal     1
2 Other     2
3 Other     3
4 Fatal     4
5 Other     5
6 Other     6

然后你可以进行额外的处理,例如:

> library(dplyr)
> test_df %>% group_by(x) %>% summarize(n)
# A tibble: 2 x 2
  x         n
  <fct> <dbl>
1 Fatal  45.0
2 Other  45.0