1

Here is my example data set.

Name    Type    B     C     D
Carl    AB      1     0     2
Carl    AB      5     4     1 
Joe     B       0     3     1
Joe     O       2     1     0
Joe     B       4     4     2 

My goal is to calculate the average of column B as a function like: someFunction(Name,Type)

For example, someFunction(Carl,AB) = 3 and someFunction(Joe,B) = 2

Does anyone know how I would go about doing so?

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Amanda
  • 75
  • 1
  • 9
  • Maybe a function is not necessary for this particular case. You can simply type: `mean(dat$B[dat$Name == "Joe"])` – s_baldur Jan 19 '17 at 15:53

2 Answers2

1

We can use a function to get the subset of B based on the string input in the arguments of the function and subsequently get the mean

f1 <- function(str1, str2){
        mean(subset(dat, Name == str1 & Type ==str2, select = B)[,1])
 }

f1("Carl", "AB")
#[1] 3

f1("Joe", "B")
#[1] 2

Update

If we need to have the mean column name also as argument,

f2 <- function(str1, str2, meanCol){
     mean(dat[dat$Name ==str1 & dat$Type == str2, meanCol])
}

f2("Carl", "AB", "B")
#[1] 3

data

dat <- structure(list(Name = c("Carl", "Carl", "Joe", "Joe", "Joe"), 
Type = c("AB", "AB", "B", "O", "B"), B = c(1L, 5L, 0L, 2L, 
4L), C = c(0L, 4L, 3L, 1L, 4L), D = c(2L, 1L, 1L, 0L, 2L)),
 .Names = c("Name", 
"Type", "B", "C", "D"), class = "data.frame", row.names = c(NA, 
-5L))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you. This is giving me an answer I believe it too high, what does the [,1] mean in your code? – Amanda Jan 19 '17 at 15:53
  • @Amanda I thought you wanted a specific answer.(Based on your earlier comment on the other question). The `[,1]` is just to subset the data.frame column into a vector as `mean` will work only a `vector` and not on `data.frame` – akrun Jan 19 '17 at 15:55
  • Hmm, so does that mean if I wanted to get the mean in column c instead of b, does this change the `[,1]` ? – Amanda Jan 19 '17 at 16:02
  • @Amanda THis is based on the arguments that you showed in the function in your post. If you want to have the columns also dynamic, have one more argument. The `[,1]` is for converting the 'data.frame' of one column into vector – akrun Jan 19 '17 at 16:08
  • Thank you. If my column has negative values, will these be added up correctly before they are divided by the total count (to find the mean)? – Amanda Jan 19 '17 at 16:48
0

This calculates the mean value for the unique combinations of Name and Type:

dat %>% group_by(Name, Type) %>% summarise(mn = mean(B))
Source: local data frame [3 x 3]
Groups: Name [?]

   Name  Type    mn
  <chr> <chr> <dbl>
1  Carl    AB     3
2   Joe     B     2
3   Joe     O     2

From here you could get the values you need.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149