使用ifelse / if时将因子设置为字符串

时间:2016-04-26 07:01:24

标签: r

数据'abc'如下 -

Org_Region  Dest_Region Org_City    Dest_City
WEST    EAST    BHI AAA
NORTH   WEST    DEL BHI
SOUTH   EAST    SHD AAA
WEST    NORTH   BHI ABA
NORTH   NORTH   DEL ABA
SOUTH   NORTH   SHD ABA
WEST    NORTH   BHI ABA
WEST    WEST    BHI BHI
NORTH   SOUTH   DEL ADB
SOUTH   SOUTH   SHD ADB

在上面的数据中,我想介绍一个名为'Region'的新列,它看起来像一个excel公式 -

IF(Org_City=Dest_City,"Same City",IF(Org_Region= Dest_Region,"Same Region","Rest"))

当我尝试以下语法时 -

abc$Region <- with(abc, ifelse(abc$Org_City == abc$Dest_City, "Same City", ifelse(abc$Org_Region == abc$Dest_Region, "Same Region", "Rest")))

我得到的错误是 -

  

Ops.factor中的错误(abc $ Org_City,abc $ Dest_City):级别设置为   因素是不同的

2 个答案:

答案 0 :(得分:3)

abc[] <- lapply(abc, as.character)
with(abc, ifelse(Org_City == Dest_City, "Same City", 
                              ifelse(Org_Region == Dest_Region, "Same Region", "Rest")))
# [1] "Rest"        "Rest"        "Rest"        "Rest"      
#       "Same Region" "Rest"        "Rest"        "Same City"   "Rest"        "Same Region"

旁注。使用with

时,无需为每个变量指定数据框

如果我们想保留因素,那么:

ifelse(as.character(abc$Org_City) == as.character(abc$Dest_City),
       "Same City",
       ifelse(as.character(abc$Org_Region) == as.character(abc$Dest_Region),
              "Same Region", "Rest"))

答案 1 :(得分:1)

尝试以下方法: 首先从因子转换为字符串:

dataframe$Org_Region = as.character(dataframe$Org_Region)
dataframe$Dest_Region = as.character(dataframe$Dest_Region)

然后定义一个函数并使用sapply

define_region <- function(org_city,dest_city){
if(org_city == des_city){
Region = "Same city"
} else{
Region = "Rest"}
}
dataframe$Region = sapply(dataframe$org_city,define_region,dataframe$dest_city,USE.NAMES = FALSE)