简体   繁体   中英

Is there any R/ shell/Perl/python code to calculate average of 3rd column based on first and 2nd column

We need a code R/shell/Perl/python to calculate average of 3rd column based on first and 2nd column. We have large data so need a code/command. Here are my input sample with required output format.

[Input_data_sample][1]
Gene_IDs    Tissues     Expressions
A   X   2
A   X   3
A   Y   2
B   X   3
B   X   4
B   Y   5
B   Y   2
C   X   3
C   X   2
C   Y   3
C   Y   2
C   Y   3
D   X   2
D   Y   2
D   Y   3

Expected Output wee need

[Expected_Output][2]
Gene ID     Tissue         Expression_average_of each tissue_for_each_Gens_IDs
A                  X                 2.5
A                  Y                 2
B                  X                 3.5
B                  Y                 3.5

Here you go. I've used the data in your post. It's R code using the dplyr library from tidyverse package.

> library(dplyr)
> df = read.table(text="Gene_IDs    Tissues     Expressions
+ A   X   2
+ A   X   3
+ A   Y   2
+ B   X   3
+ B   X   4
+ B   Y   5
+ B   Y   2
+ C   X   3
+ C   X   2
+ C   Y   3
+ C   Y   2
+ C   Y   3
+ D   X   2
+ D   Y   2
+ D   Y   3
+ ",  header=T) %>% tibble()
> df
# A tibble: 15 × 3
   Gene_IDs Tissues Expressions
   <chr>    <chr>         <int>
 1 A        X                 2
 2 A        X                 3
 3 A        Y                 2
 4 B        X                 3
 5 B        X                 4
 6 B        Y                 5
 7 B        Y                 2
 8 C        X                 3
 9 C        X                 2
10 C        Y                 3
11 C        Y                 2
12 C        Y                 3
13 D        X                 2
14 D        Y                 2
15 D        Y                 3
> df %>% group_by(Gene_IDs, Tissues) %>% summarise(avg = mean(Expressions))
# A tibble: 8 × 3
# Groups:   Gene_IDs [4]
  Gene_IDs Tissues   avg
  <chr>    <chr>   <dbl>
1 A        X        2.5 
2 A        Y        2   
3 B        X        3.5 
4 B        Y        3.5 
5 C        X        2.5 
6 C        Y        2.67
7 D        X        2   
8 D        Y        2.5 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM