r - simplify multiple one to one paired aggregation -


I want to calculate the mean for one coupled one by one. For example, I have data. The frame is below I column I b1 to sym & amp; I want to calculate the mean; a1 , and b2 by sym & amp; a2 together

  sym a1 a2 b1 b2 1 a 1 2 1 1 2 a 2 2 2 2 3 a1 2 3 3 4 a 2 2 4 4 5 b 1 1 5 5 6 b 2 1 6 6 7b 1 7 7 8b 2 1 8 8  

This is my code that repeats the lapply more than every couple what else than this Is there a more efficient way?

  df < - data.frame (sym = c (representative ('a', 4), representative ('b', 4)), a1 = representative (1: 2, 4), a2 = representative (2: 1, each) = 4), B1 = Representative (1: 8), B2 = Representative (1: 8)) TMP < - ddply (df, "sym", function (x) {temp.ls} - lapply (1: 2, function (i) {t2 & lt; - total (x = x [3 + i], by = X [1 + i], (= a), "b" t2}) temp.all  

text after

dplyr makes it very simple:

  library (dplyr) inner_join (df% & gt;% group_by (symbol, A1)% & gt;% short (b1.mean = Meaning (B1)), df% & gt;% group_by (symbol, A2)% & gt;% summation (b2.mean = mean (b2))) #: "sym" #source : Local data frame [4 x 5] # Groups: We # CIM A1 B1. Mn a2b2 IMI # 1A1 2 2.5 2.5 # 2A 2 3 2 2.5 # 3B1 6 1 6.5 # 4 B2 7 1 6.5  

If you type a Want a column, and want to fill the non-displaying combinations with NA in your example solution, then left_join is an option:

  left_join (df% & gt;% group_by (sym, a = a1)% & gt;% short (Blkmean = mean (B 1)), df% & gt;% group_by (symbol, a = # Source: Local data frame [4 x 4] # Groups: SIM # # (B2); =% (; 2), by c = ('symbol', 'a')) SIM A1 Mean B2. Pisces # 1 a1 2 na # 2 a23 2.5 # 3b1 6 6.5 To remind me about the  dplyr   joining  # 4b2 7a  

Hat-tip @beginnerR

Edit

In response to comments, if you have more than two groups, and in the resulting tables together all , then there is a way to do this:

  # Example data set .seed (1) (d & lt; - Data.frame (sym = sample ( Character [1: 4], 10, change = t), a1 = sample (5, 10, location = TRUE), a2 = sample (5, 10, location = TRUE), a3 = replacing sample (5, 10, = TRUE), b1 = runif (10), b2 = runif (10), b3 = runif (10))) # symbols a1 a2 a3b1b2b3 # 1b2 5 3 0.82, 9, 463, 0.4, 77, 61, 9 6 0.9, L2,87,5 9 2 # 2 B 1 2 3 0.6470602 0.86120948 0.29360337 # 3 of 4 4 3 0.7829328 0.43809711 0.45906573 # 4 D 2 1 1 0 0,5530363, 24479728 0,33239467 # 5 4 2 5 0.5297196 0.07067905 0.65087047 # 6 d 3 2 4 0.7893562 0.09946616 0.25801678 # 7 d 4 1 4 0.0233312 0.31627171 0.47854525 # 8 c 5 2 1 0 , 4772301 0,5l863426 0,7663l067 # 9 c 2 5 4 0.7323137 0.66200508 0.08424691 # 10 4 2 3 0.6 9, 27316 0k4,06,83,0l 9 0k8,75, 32,133 l & lt; - mapply (function (x, y) {grpd & lt; -eval (option (by group = (D, C, SM, A = X), list (x = like .name (x))) eval (option) Paste 0 ('B', 1: 3), Paste 0 ('B', 1: 3), SIMPLIFY = ('A', ' FALSE) (Function (...) left_join (..., all = t), L) # Source: Local data frame [9 x 5] # Group: SIM # # SIM means a mean (B1) B2) Mean (B3) # 1A4 0.6112256 NA # 2B1 0.6470602 NA NO. 3B 2 0,820 9 463 0.86120 9 48 NA # 4 C2 0.7323137 0.51863426 NA # 5 C 4 0.7829328 0.43809711 0.08424691 # 6 C5 0.4772301 0.66200508 NA # 7 D2 0.5530363 0.09946616 NA # 8 D3 0.7893562 NA # 9 D 4 0.0233312 NA 0.36828101  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -