Suppose I have a vector containing a sequence of radii in a, and the area and circumference for radii from 1 through 30 in circle_stats. I want to subset circle_stats such that the rows match those found in a.
a <- as.character(c(1,2,3,4,5,5,6,7,2,8,4,9,10,10,1,1,1,1,1,4))
unique(a[duplicated(a)])
# these are the duplicated values
# 5 2 4 10 1
radius <- c(1:30)
circle_stats <- data.frame(radius=as.character(radius),
area=pi*radius^2,
circumference=2*pi*radius)
This would also mean being able to keep duplicates instead of discarding them, or 20 rows subsetted from circle_stats instead of just 10.
This is what I tried:
library(data.table)
circle_stats %>%
subset(radius %in% a) %>%
arrange(match(radius, a))
radius area circumference
1 1 3.141593 6.283185
2 2 12.566371 12.566371
3 3 28.274334 18.849556
4 4 50.265482 25.132741
5 5 78.539816 31.415927
6 6 113.097336 37.699112
7 7 153.938040 43.982297
8 8 201.061930 50.265482
9 9 254.469005 56.548668
10 10 314.159265 62.831853
As you can see, the issue is that I can't keep duplicates, which I assume requires an argument or function within the subset function. I just can't figure out which one.
Any help is greatly appreciated!
edit: originally the question considered numerical values, but the dataset I'm working with uses character values in a and circle_stats$radius.
Thank you @lotus for the answer: