这是我对这个问题的尝试,但是我不确定自己在做什么错。
Gender <- c('Male','Male','Male', 'Female', 'Female', 'Female')
Height <- c('183', '180', '185', '170', '165', '167')
DF <- data.frame(Gender, Height)
Male <- DF[(DF$Gender == 'Male'),]
Exclude_male <- DF[!Male]
答案 0 :(得分:2)
我不确定问题的确切含义,但是您可以做的一件事是定义一个逻辑向量,该逻辑向量对于男性行为TRUE,对于女性行为FALSE:
is_male = DF$Gender == 'Male'
# note this is the same code you used in your "Male <-" line,
# I'm just giving a name to a smaller part of it so we can re-use it
# Now you can define the `Male` and `Female` subsets using `is_male`:
Male = DF[is_male, ]
Female = DF[!is_male, ]
答案 1 :(得分:1)
另外两个解决方案,都带有subset
。
Female1 <- subset(DF, Gender != 'Male')
Female2 <- subset(DF, Gender == 'Female')
identical(Female1, Female2)
#[1] TRUE
答案 2 :(得分:0)
这是用于保留所需值的其他代码选项。
Only_female <- subset(DF, Gender == 'Female', select = c(Gender, Height))