如何根据另一列创建因子列?

时间:2018-03-08 20:17:52

标签: r

我想创建一个名为" Region"的列。基于"社区面积"的值,例如社区面积1 =北,社区面积2 =南。 我希望它是这样的:

MvcEvent

我尝试了以下代码,但没有帮助:

Community area   Region
25               West
67               Southwest
39               South
40               South
25               West

2 个答案:

答案 0 :(得分:0)

对于涉及ifelse if的长表达式,请尝试case_when包中的dplyr

> set.seed(1234)
> 
> df <- data.frame(x1 = round(runif(n = 20, min = 1, max = 4), 0), stringsAsFactors = F)
> 
> df
   x1
1   1
2   3
3   3
4   3
5   4
6   3
7   1
8   2
...
20  2
> 
> df$Region <- dplyr::case_when(df$x1 == 1 ~ "North", 
+                  df$x1 == 2 ~ "South", 
+                  df$x1 == 3 ~ "East",
+                  TRUE ~ "West")
> df
   x1 Region
1   1  North
2   3   East
3   3   East
4   3   East
5   4   West
6   3   East
7   1  North
...
20  2  South

答案 1 :(得分:0)

可以通过修改OP功能在region想法中实现一个解决方案。

  # Take one value at a time and return Region
  region<-function(x){if(x %in% c(8,32,33)){"Central"} 
    else if(x %in% c(5,6,7,21,22)){"North"}
    else if(x %in% c(1:4,9:14,76,77)){"Far North Side"}
    else if(x %in% c(15:20)){"Northwest Side"}
    else if(x %in% c(23:31)){"West"}
    else if(x %in% c(34:43,60,69)){"South"}
    else if(x %in% c(56:59,61:68)){"Southwest Side"}
    else if(x %in% c(44:55)){"Far Southeast Side"}
    else if(x %in% c(70:75)){"Far Southwest Side"}
    else {"Other"}
  }

# Use mapply to pass each value of `Community_area` to find region as
df$Region <- mapply(region, df$Community_area)

df
#  Community_Area         Region
#1             25           West
#2             67 Southwest Side
#3             39          South
#4             40          South
#5             25           West

数据

df <- data.frame(Community_Area = c(25, 67, 39, 40, 25))