[英]how to keep rows based on first digit of elements in r ?
I have the following data.frame 我有以下data.frame
crime<-c(71040,142320,71013,71013,72113)
coded.month<-c("2018-10","2018-10","2018-10","2018-10","2018-10")
df<-data.frame(coded.month,crime)
coded.month crime
1 2018-10 71040
2 2018-10 142320
3 2018-10 71013
4 2018-10 71013
5 2018-10 72113
Bascially, I want to isolate all the rows where the first digit of crime is 7
so that I get the following 基本上,我想隔离犯罪的第一位数字为7
所有行,以便获得以下信息
coded.month crime
1 2018-10 71040
3 2018-10 71013
4 2018-10 71013
5 2018-10 72113
how do I go about this? 我该怎么办?
You may use substr
: 您可以使用substr
:
df[substr(df$crime, 0, 1) == 7, ]
# coded.month crime
# 1 2018-10 71040
# 3 2018-10 71013
# 4 2018-10 71013
# 5 2018-10 72113
We can also use %/%
我们也可以使用%/%
df[df$crime%/% 10000 == 7, ]
# coded.month crime
#1 2018-10 71040
#3 2018-10 71013
#4 2018-10 71013
#5 2018-10 72113
using startsWith
: 使用startsWith
:
subset(df, startsWith(as.character(crime),"7"))
# coded.month crime
# 1 2018-10 71040
# 3 2018-10 71013
# 4 2018-10 71013
# 5 2018-10 72113
This also involves converting values (implicitly) to strings, but it works: 这还涉及将值(隐式)转换为字符串,但是它可以:
df[grep("^7", df$crime), ]
Edit: a purely numerical solution: 编辑:纯数值解:
df[floor(df$crime / 10^floor(log10(df$crime))) == 7, ]
By defining a new dataframe using grepl()
to match only those df$crime
values that start with "7": 通过使用grepl()
定义新的数据grepl()
使其仅匹配以“ 7”开头的df$crime
值:
df_new <- df[grepl("^7", df$crime, perl = T),]
df_new
coded.month crime
1 2018-10 71040
3 2018-10 71013
4 2018-10 71013
5 2018-10 72113
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.