Is the following situation possible in R?
Here is some data and a function:
library("dplyr")
df <- data.frame(b = rnorm(100,5,5), d = rnorm(100,2,2),
c = rnorm(100,10,10))
a <- c("a", "b", "c", "d", "e")
a <- sample(a, 100, replace=TRUE, prob=c(0.3, 0.2, 0.3, 0.1, 0.1))
a<- as.factor(a)
df$a = a
> head(df)
b d c a
1 3.1316480 0.5032860 4.7362991 a
2 4.3111450 -0.1142736 -0.5841322 c
3 2.8291346 3.6107839 16.0684492 a
4 14.2142245 4.9893987 -1.8145138 a
5 -6.7381302 0.0416782 -7.7675387 c
6 0.4481874 0.3370716 17.4260801 a
Here is the function:
my_subset_mean <- function(r1, r2, r3){
subset <- df %>% filter(a %in% r1, b > r2, d < r3)
return(mean(subset$c))
}
I can evaluate this function with the following inputs:
my_subset_mean(r1 = c("a", "b"), r2 = 5, r3 = 1 )
[1] 11.46365
Question: But is it possible to do the following in R?
#example 1
my_subset_mean(r1 = NA , r2 = 5, r3 = 1 )
#example 2
my_subset_mean(r1 = c("a", "b"), r2 = NA , r3 = 1 )
#example 3
my_subset_mean(r1 = NA , r2 = 5, r3 = NA )
Can someone please suggest how to do this in R?
CodePudding user response:
You could use case handling with if
clauses. Set defaults to NA
and define what should happen, e.g. use all a
levels for r1
and set r2
/r3
borders to extremes.
my_subset_mean <- function(r1=NA, r2=NA, r3=NA) {
if (all(is.na(r1))) r1 <- df$a
if (is.na(r2)) r2 <- -Inf
if (is.na(r3)) r3 <- Inf
s <- subset(df, a %in% r1 & b > r2 & d < r3)
return(mean(s$c))
}
my_subset_mean(r1 = c("a", "b"), r2 = 5, r3 = 1 )
# [1] 10.50051
my_subset_mean(r1 = NA , r2 = 5, r3 = 1 )
# [1] 11.0134
my_subset_mean(r1 = c("a", "b"), r2 = NA , r3 = 1 )
# [1] 9.024224
my_subset_mean(r1 = NA , r2 = 5, r3 = NA )
# [1] 12.84011
Data
df <- structure(list(b = c(9.69843582599532, 8.46269535731819, 4.82407895858266,
6.58444098769932, -1.90602290454891, 0.62137343811342, 0.246206065813347,
0.793036653531213, 8.77121342495327, -7.44132891058167, 6.0767980203778,
11.2931613920878, 3.10188736172521, 8.58460261433062, 4.30000646000838,
11.1761679757397, 10.1886880475286, 4.39613023166472, 6.62616108371428,
7.12973461820176, 7.06418042730859, 1.41813294429105, 4.45124874431744,
9.06270943595514, 2.85615985307963, 11.3675141884702, 12.4605228304222,
-2.62942853280514, -1.05513620642596, -2.82466757798307, 5.47870836189876,
15.4452896936201, 3.49398693389683, 8.88806953506397, 11.5004092126707,
8.45253517064691, -4.97987424572937, 11.4487536566939, 10.2306454615809,
2.5945245314969, 6.3131412115584, 6.69491387932119, 9.81906160221122,
1.8232549239818, 9.28993104675214, 10.4049121140586, -0.0663306550603622,
9.43597782121374, -2.69618206231453, 9.81899536383879, 6.65283190982383,
2.62705531844689, 0.0506821013280137, 1.71469478379078, -2.00948886925582,
7.11033909303673, 3.6342673960314, -3.85199889555037, 3.837719577011,
10.5379173614526, 0.241165053988417, 2.98878226744129, 16.5338502557644,
6.40322268215467, 3.09682071245305, 6.4640882790184, 10.2295103168259,
-3.54258794150942, 3.57412615661292, 0.847157363296616, 0.958937497233153,
9.54449804351409, 2.13358415511275, -4.64977595405939, 13.8692102497496,
12.0947702962328, 10.0054091647506, 5.95099130951445, 13.9878643818839,
16.656899833379, -2.01250530111107, 10.9738378550877, 5.70649824688106,
6.23841756803367, 7.89201847081608, 7.43865691287543, 4.25969916370806,
0.100094919396823, 12.1619498695491, 6.70521010911249, 5.23952320864633,
6.27893660411319, 1.67615931536556, 8.47111597398358, 10.7407384583375,
6.6935988330649, 12.8064551325813, 5.06822778652961, 6.18969940741962,
8.45826815718404), d = c(0.576695372501562, 2.02403852307891,
4.47302910094982, 1.46591817375579, -1.90112821827422, 2.41840031582007,
3.90092963341364, 3.1470221849379, 2.39506516190426, 3.18366311180243,
4.59374236726957, -0.310602591211496, 0.89296227766288, -2.60780517131932,
3.29393680957706, 1.28194042737137, 4.06048318268274, 2.44158570826126,
1.05329331360239, -0.149166155095435, 0.777410699269984, 0.720966662138413,
3.07192930199898, 1.19472321229584, 1.6887709685994, 0.967254843444039,
-0.094671269353285, 2.45712134247711, 0.506684056027063, 2.61908201167241,
-0.382807099233013, 2.76118352895658, 4.11734869309253, 2.6251159565002,
0.890320607632241, 1.31450894352574, -0.162543197930164, -1.245287729716,
7.54736588980002, 2.50874311708861, 2.42852224699317, 0.396696115747124,
-2.55570995185874, 2.95066460269776, -1.86568489159839, 1.62344539230723,
1.38481792508596, 3.92664646129847, 0.798194771588095, 3.02349013522507,
3.52584742333097, 0.394636796366123, 0.908953662919601, 1.68995871697649,
3.78699256158826, 3.48105744707364, 0.289938311962986, -1.65615141140493,
1.73573322264205, 3.3817736811699, 4.83023278077458, -0.00341030149943089,
-1.9186029474108, 4.62319725720613, 1.70867185021965, 0.890015362641153,
-1.23390286977168, 3.04207184098186, -0.831917884053825, 4.08268109740385,
1.55442789102912, 2.387712200986, 2.98273743020262, 1.20107865472766,
3.91106586582941, 2.83324789816271, 1.78539821303068, -0.601442920761817,
1.60782713132619, 0.972339568265609, 2.72048427045796, 2.08343673583862,
2.59627494701068, 0.898957520125343, 3.03113875095081, 2.29259741051018,
0.48924176893708, 2.21502767325415, -0.699757523015137, 1.86249088436291,
-1.00274924638415, 0.984005479892101, -3.11310545034708, 2.72294398534204,
-1.81783882908668, 2.8517333971145, -3.65063013833178, 2.63550421209849,
-2.10770259607717, -0.830415297865299), c = c(17.019003845757,
13.3617768297152, 4.48066238288829, 37.4718618472645, 11.1069019480034,
1.75616658645729, 0.624475582732705, 15.5817026692234, 22.6629232046836,
18.2802980877926, 6.00913771363243, 8.15814374806181, 9.31214173358088,
17.0895672083431, 0.775439933083602, 23.4787728977868, -0.350463197592063,
16.8900893828887, 23.3015857650274, 1.12866295467943, 15.5354333129547,
5.75102947515029, 4.42405750345341, 10.3803737245953, 0.0651555296711877,
6.38910987546489, 30.0933493405791, 13.740864245639, 11.5558739730914,
24.4726877624024, 10.8607475395052, 5.25398069855701, 8.94458272243297,
7.25290093283984, 11.373274598886, 17.5885131514341, -4.37803727480431,
17.0367932953449, 24.7891045623381, 29.0664337297521, 9.63122882702153,
11.8557980079078, 31.7038297074623, 16.7253608016047, 6.76029998330636,
23.1762935502586, 12.6880697509057, 16.9108435667295, -2.22866021293843,
10.9820317029724, 18.9173027898193, -0.538613914754789, -7.5854184291672,
13.238885819089, 11.4241115746851, 15.1327418752124, 4.52304690963654,
10.5851271075696, 15.8111350966235, 4.68258123673519, 11.2056336002124,
30.4120838100663, 10.8242493781004, 6.76421873832309, -4.63649032366339,
15.5832498948434, 23.0268552127803, 25.3043736189912, 7.22967261723758,
21.4133309352144, 34.017218507608, -2.41446225948565, 0.107422788807282,
13.8812237056951, 15.1856293259466, 9.58332073588003, 26.0738826140796,
3.11530220156868, 18.9577216567341, 14.3145186996602, 2.4406205046264,
15.3651169579063, 17.7414590516619, -0.595045318185088, 22.2098320842764,
24.7643144506109, 12.4645048262384, 18.1943363251665, 7.71355198959901,
-0.585098904800947, -7.66751421801502, 14.2940217046492, -2.74480357019551,
12.1894293122497, 4.44579027604645, -0.191526624075939, 11.3876592877874,
14.9409323605176, -3.30763960516194, 8.20930867705211), a = structure(c(4L,
2L, 2L, 5L, 4L, 1L, 1L, 4L, 2L, 4L, 3L, 3L, 4L, 1L, 3L, 3L, 5L,
1L, 4L, 1L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 3L, 5L, 3L, 2L, 1L,
4L, 3L, 4L, 3L, 3L, 1L, 4L, 1L, 5L, 5L, 3L, 3L, 3L, 5L, 2L, 3L,
2L, 2L, 4L, 2L, 3L, 3L, 5L, 2L, 3L, 2L, 2L, 2L, 1L, 5L, 1L, 1L,
4L, 5L, 1L, 2L, 3L, 2L, 3L, 3L, 3L, 4L, 1L, 3L, 1L, 2L, 2L, 5L,
2L, 2L, 3L, 3L, 1L, 3L, 1L, 4L, 1L, 5L, 1L, 1L, 1L, 3L, 2L, 1L,
2L, 1L, 3L), .Label = c("a", "b", "c", "d", "e"), class = "factor")), row.names = c(NA,
-100L), class = "data.frame")