I'm relatively new in R. I have this problem. I have data of dogs example of useful part of data (columns age_month and rasnaam (breed) are used)
I have to look for all the breeds if they are small, medium, large etc. And if they are a small breed then all the rows where age_month is lower than 9 have to be removed, if they are a medium sized breed rows where age_month is lower than 13 have to be removed, (large, age_month < 24). I've tried some things but it won't work. I've added all dogs to a list (also tried it with vector) like this: (only for small dogs here)
small_dogs <- list("Affenpinscher", "Bichon frisé", "Bolognezer", "Chihuahua, langhaar",
"Dandie Dinmont Terrier", "Dwergkeeshond", "Japanse Spaniel",
"Griffon belge", "Griffon bruxellois", "Kleine Keeshond",
"Lhasa Apso", "Maltezer", "Mopshond", "Pekingees", "Petit Brabançon",
"Shih Tzu", "Tibetaanse Spaniel", "Volpino Italiano", "Yorkshire Terrier")
I tried this:
for (i in 1:nrow(brachquest2)){
ifelse((brachquest2$rasnaam %in% small_dogs), (brachquest2 <- brachquest2[!(brachquest2$age_month < 9), ]),
ifelse((brachquest2$rasnaam %in% medium_dogs)), (brachquest2 <- brachquest2[!(brachquest2$age_month < 13), ]),
(brachquest2 <- brachquest2[!(brachquest2$age_month < 24), ]))
}
But then I get an unused arguments error. Then I tried to use case_when(), but I'm not familiar with this function, so maybe I'm using it awfully wrong:
brachquest2 <- case_when(
brachquest2$rasnaam %in% small_dogs ~ brachquest2[!(brachquest2$age_month < 11), ],
brachquest2$rasnaam %in% medium_dogs ~ brachquest2[!(brachquest2$age_month < 13), ]
)
Then I get an error: must be length 66 or one, not 18.
(the number of rows is 66)
I hope I explained it okay. Does someone have some useful tips for me? Or maybe it could be much simpler, every help is appreciated!! Thanks in advance
Below is dput of only age_month and rasnaam in reaction to neilfws. I don't know for sure if this is the right way
structure(list(age_month = structure(c(50, 52, 52.1, 49.7, 49.7,
49.6, 49.6, 49.6, 49.5, 50, 48.8, 52.1, 51.9, 48.7, 50, 50.2,
50.4, 50.5, 49, 49, 49, 49, 49, 48.9, 15, 17.6, 17.6, 17.6, 17.6,
16.3, 17.6, 17.6, 15, 15.8, 16, 16.2, 17.5, 14.9, 10.4, 10.2,
10.5, 10.4, 10.3, 10.3, 10.2, 10.3, 10.3, 10.3, 12.8, 12.8, 12.8,
12.8, 12.8, 10, 10.4, 10.2, 10.3, 10.3, 12.7, 12.7, 13.2, 13.2,
13.1, 13.1, 12.7, 12.7), units = "days", class = "difftime"),
rasnaam = c("American Staffordshire Terrier", "Boxer", "Bull Terrier",
"Chihuahua, langhaar", "Chihuahua, langhaar", "Chihuahua, langhaar",
"Chihuahua, langhaar", "Chihuahua, langhaar", "Chihuahua, langhaar",
"Chihuahua, langhaar", "Franse Bulldog", "Franse Bulldog",
"Labrador Retriever", "Shih Tzu", "American Staffordshire Terrier",
"American Staffordshire Terrier", "American Staffordshire Terrier",
"American Staffordshire Terrier", "American Staffordshire Terrier",
"American Staffordshire Terrier", "American Staffordshire Terrier",
"American Staffordshire Terrier", "American Staffordshire Terrier",
"American Staffordshire Terrier", "American Staffordshire Terrier",
"Boxer", "Boxer", "Boxer", "Boxer", "Boxer", "Bull Terrier",
"Bull Terrier", "Chihuahua, langhaar", "Chihuahua, langhaar",
"Chihuahua, langhaar", "Chihuahua, langhaar", "Chihuahua, langhaar",
"Franse Bulldog", "Franse Bulldog", "Franse Bulldog", "Franse Bulldog",
"Franse Bulldog", "Labrador Retriever", "Labrador Retriever",
"Labrador Retriever", "Labrador Retriever", "Labrador Retriever",
"Labrador Retriever", "Labrador Retriever", "Labrador Retriever",
"Labrador Retriever", "Labrador Retriever", "Labrador Retriever",
"Shih Tzu", "Shih Tzu", "Shih Tzu", "Shih Tzu", "Shih Tzu",
"American Staffordshire Terrier", "Boxer", "Franse Bulldog",
"Franse Bulldog", "Shih Tzu", "Shih Tzu", "American Staffordshire Terrier",
"Boxer")), row.names = c(NA, -66L), class = "data.frame")