[英]How to create a factor column based on another column?
我想基于“社区区域”的值创建一个名为“区域”的列,例如,社区区域1 =北,社区区域2 =南。 我希望它是这样的:
Community area Region
25 West
67 Southwest
39 South
40 South
25 West
我尝试了以下代码,但没有帮助:
region<-function(x){if(x==c(8,32,33)){crime$Region<-"Central"}
else if(x==c(5,6,7,21,22)){crime$Region<-"North"}
else if(x==c(1:4,9:14,76,77)){crime$Region<-"Far North Side"}
else if(x==c(15:20)){crime$Region<-"Northwest Side"}
else if(x==c(23:31)){crime$Region<-"West"}
else if(x==c(34:43,60,69)){crime$Region<-"South"}
else if(x==c(56:59,61:68)){crime$Region<-"Southwest Side"}
else if(x==c(44:55)){crime$Region<-"Far Southeast Side"}
else if(x==c(70:75)){crime$Region<-"Far Southwest Side"}
else {crime$Region<-"Other"}
}
region(crime$Community.Area)
对于涉及if
和else 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
通过修改region
功能,可以在OP
理念中实现一种解决方案。
# 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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.