[英]filtering data.frame character column in r
I have a variable term which includes loan term and has only two types: "36 months" and "60 months".我有一个可变期限,其中包括贷款期限,并且只有两种类型:“36 个月”和“60 个月”。 I want to filter my dataset df and create two different subsets for these categories.
我想过滤我的数据集 df 并为这些类别创建两个不同的子集。
I have tried using the commands subset, filter,which etc but it didn't work.我曾尝试使用命令子集、过滤器、哪个等,但没有用。
codes that I have tried df1 –> df[which(df$term == "36 months"),]
, df1 –> filter(df, term == "36 months")
df1–>df[df$term %in% c("36 months"), ]
and other ways but always choices zero rows.我尝试过的代码
df1 –> df[which(df$term == "36 months"),]
, df1 –> filter(df, term == "36 months")
df1–>df[df$term %in% c("36 months"), ]
等方式,但总是选择零行。
head(loan_data$term)
[1] " 36 months" " 60 months" " 36 months" " 36 months" " 60 months" " 36 months" [1]“36个月”“60个月”“36个月”“36个月”“60个月”“36个月”
Any suggestion?有什么建议吗?
Thanks谢谢
I cannot reproduce your problem:我无法重现您的问题:
df <- data.frame(
id = c(1, 2, 3),
val = c("60 months", "36 months", "60 months")
)
df[df$val == "60 months", ]
# df$val == "60 months"
# 1 1 60 months
# 3 3 60 months
df[which(df$val == "60 months"),]
# df$val == "60 months"
# 1 1 60 months
# 3 3 60 months
as more robustly explained here , using...正如这里更强有力地解释的那样,使用...
my.data.frame %>% filter(.data[[myName]] == 1) ... my.data.frame %>% filter(.data[[myName]] == 1) ...
df60 <- df %>% filter(.df[[term]] == "60 months")
df30 <- df %>% filter(.df[[term]] == "30 months")
ssP <- mutate( nino, PHASE = ifelse(PHASE == "E", "N","L"))
which for your df ..这对于您的 df ..
df60 <- mutate( df, term = ifelse(term == "60 months"))
df30 <- mutate( df, term = ifelse(term == "30 months"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.