Reputation: 11
I have what may be a very simple problem to solve in R, but I've been struggling with it for a while without finding an adequate solution.
From the following dataframe, I just need to fully remove any row that contains 'U' for Sex, whilst also removing the level 'U' from factor Sex. And I would also like the ability to remove any ID of my choosing by specifying a vector of ID numbers. My actual dataset is much larger than this.
datf <- read.table(text = "ID Sex Mor SITE
110 F W 1
111 M W 2
112 M B 4
135 F W 3
556 M B 1
557 U B 1
558 M W 2",
header = TRUE)
I thought I would be able to work this out, but I have tried various methods suggested on this website such as droplevels- but I can't seem to figure it out. Thanks very much in advance for your help!
Upvotes: 0
Views: 10482
Reputation: 717
Remove rows with Sex == 'U'
and drop level
datf <- droplevels(datf[!datf$Sex == 'U',])
str(datf)
'data.frame': 6 obs. of 4 variables:
$ ID : int 110 111 112 135 556 558
$ Sex : Factor w/ 2 levels "F","M": 1 2 2 1 2 2
$ Mor : Factor w/ 2 levels "B","W": 2 2 1 2 1 2
$ SITE: int 1 2 4 3 1 2
Remove any row where ID in vector
datf <- datf[!(datf$ID %in% c(110, 558)),]
datf
ID Sex Mor SITE
2 111 M W 2
3 112 M B 4
4 135 F W 3
5 556 M B 1
Upvotes: 3