我的数据格式如下:
ID Species Sex
1 spA M
2 spB F
3 spA Sex Required
我想写一行代码,它给出了包含" Sex Required"的所有行的整行。在性别专栏
我试过了:
df[df$Sex == "Sex Required",]
但它无法正常工作。我知道这可能很简单,但我是R.的新手。
答案 0 :(得分:0)
尝试:
df<- structure(list(ID = 1:3, Species = structure(c(1L, 2L, 1L), .Label = c("spA",
"spB"), class = "factor"), Sex = structure(c(2L, 1L, 3L), .Label = c("F",
"M", " Sex Required"), class = "factor")), .Names = c("ID", "Species",
"Sex"), class = "data.frame", row.names = c(NA, -3L))
我认为有一些空格,即“需要性别”,或“需要性别”
df[df$Sex == "Sex Required",]
#[1] ID Species Sex
#<0 rows> (or 0-length row.names)
df[grep("Sex Required", df$Sex),]
# ID Species Sex
# 3 3 spA Sex Required
或者
library(stringr)
df$Sex <- str_trim(df$Sex)
df[df$Sex == "Sex Required",]
# ID Species Sex
#3 3 spA Sex Required