[R] subset data frame problem
Richard O'Keefe
r@oknz @end|ng |rom gm@||@com
Mon Dec 13 13:00:20 CET 2021
You want to DELETE rows satisfying the condition P & Q.
The subset() function requires an expression saying what
you want to RETAIN, so you need subset(PD, !(P & Q)).
test <- subset(PD, !(Class == "1st" & Survived == "No"))
By de Morgan's laws, !(P & Q) is the same as (!P) | (!Q)
so you could also write
test <- subset(PD, Class != "1st" | Survived != "No")
I'd actually be tempted to do this in two steps:
unwanted <- PD$Class == "1st" & PD$Survived == "No"
test <- PD[!unwanted,]
On Mon, 13 Dec 2021 at 17:30, Kai Yang via R-help <r-help using r-project.org>
wrote:
> Hi R team,I want to delete records from a data frame if Class = '1st' and
> Survived = 'No'. I wrote the code below, test <- subset(PD, Class != '1st'
> && Survived != 'No')
> but the code return a wrong result. Can someone help me for this?
> Thanks,Kai
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list