我有一个名为df的数据框,我需要使用filter()从数据框中过滤数据。请检查
我的数据框
Queue project._id project.ProjectName project.Status project.CreatedBy project.Createdtime X.gender
first 111 Travel 1 manchi 2017-04-24 18:50:27 male
last 111 2334 1 mono 2017-04-24 18:50:27 Female
first 111 556 1 gunal 2017-04-24 18:50:27 male
first 7888 classical 1 manchi 2017-04-24 18:50:27 Female
我尝试使用dplyr过滤数据通过以下代码。
Finalfilter<-df%>%
filter(project.ProjectName == "Travel",Queue=="first")%>%
select(X.gender.)
我的预期结果是
Queue project._id project.ProjectName project.Status project.CreatedBy project.Createdtime X.gender
first 111 Travel 1 manchi 2017-04-24 18:50:27 male
first 111 556 1 gunal 2017-04-24 18:50:27 male
但我已经低于ERROR,帮我解决这个问题。
错误:列
project
必须是1d原子向量或列表
dput。
structure(list(Queue = c("first", "last", "first", "first"),
project = structure(list(`_id` = c("111", "2334", "556",
"7888"), ProjectName = c("Travel", "HBussiness", "Travel",
"classical"), Status = c(1L, 1L, 1L, 1L), CreatedBy = c("manchi",
"mono", "gunal", "manchi"), Createdtime = structure(c(1493040027.826,
1493040027.826, 1493040027.826, 1493040027.826), class = c("POSIXct",
"POSIXt"))), .Names = c("_id", "ProjectName", "Status", "CreatedBy",
"Createdtime"), row.names = c(NA, 4L), class = "data.frame"),
X.gender. = c("male", "Female", "male", "Female")), .Names = c("Queue",
"project", "X.gender."), row.names = c(NA, 4L), class = "data.frame")
答案 0 :(得分:4)
您的project.ProjectName
是数据框,而不是矢量,因此是错误。解决方法可以是,
df$project <- df$project$ProjectName
df%>%
filter(project == "Travel" & Queue=="first")%>%
select(X.gender.)
# X.gender.
#1 male
#2 male
答案 1 :(得分:1)
如果它不适用于dplyr,则另一个选择是:
df[df$project.ProjectName == "Travel" & Queue=="first", ]