calculate mean grouped by col number

Xue

I have two dataframes, df_value and df_num. df_num has two columns:row and col which are the row and column numbers of the value from df_value that meets a specific condition. I want to calculate the mean group by col in the df_num.

I got stuck at group by col.

df_value<- data.frame('a'=1:3,'b'=2:4,'c'=1:3,'d'=3:5)
df_num <- which(df<4,arr.ind = TRUE)
df_value
a b c d
1 2 1 3
2 3 2 4
3 4 3 5
df_num
row col
1    1
2    1
3    1
1    2
2    2
1    3
2    3
3    3
1    4

I want get the mean for col1, col2, col3 and col4,

mean:
 2
 2.5
 2
 3
Ronak Shah

If you have to use df_num to subset df_val and then take mean of columns one way is to split subsetted df_value based on "col" column of df_num and take mean of each list.

sapply(split(df_value[df_num], df_num[, "col"]), mean, na.rm = TRUE)

#  1   2   3   4 
#2.0 2.5 2.0 3.0 

As mentioned in the comments you can directly filter df_value to take mean

sapply(df_value, function(x) mean(x[x<4], na.rm = TRUE))

#  1   2   3   4 
#2.0 2.5 2.0 3.0 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to calculate mean of grouped dataframe?

From Dev

Calculate creation number grouped by category in Postgres

From Java

How to calculate mean values grouped on another column in Pandas

From Dev

How to calculate grand mean and standard deviation for the grouped table?

From Dev

how to calculate mean values of the past n years grouped by two variables

From Dev

Pandas: calculate mean for each row by cylcle number

From Dev

Count number of observations used to calculate the mean in SAS

From Dev

Pandas DataFrame: How to calculate the rolling mean of both grouped and shifted data in version >0.19

From Dev

Resample and select a given number of rows and calculate the mean, variance and confidence intervals?

From Dev

calculate mean and gradient of the a certain number of rows before a timestamp in another datset

From Dev

Calculate the mean number of days between dates per group

From Dev

How to calculate mean number of something per client in Teradata SQL?

From Dev

Order grouped scatterplot by mean

From Dev

Mean of grouped data

From Mysql

Calculate mode grouped by a column

From Dev

Calculate value grouped by columns

From Dev

Calculate a grouped median in pyspark

From Dev

Calculate percentage of grouped values

From Dev

Calculate mean() of Nympy 2D-array grouped by values in a separate list with strings corresponding to each row in the 2D array

From Dev

calculate mean only when the number of values in each rows is higher then certain number in python pandas

From Dev

Applying .mean() to a grouped data with a condition

From Dev

dplyr mean columns grouped by name

From Dev

adding mean to grouped violin plot

From Dev

Calculate Group Mean and Overall Mean

From Dev

Pyspark calculate a field on a grouped table

From Dev

Calculate medians of rows in a grouped dataframe

From Dev

How to calculate difference on grouped df?

From Dev

Calculate number of

From Dev

Calculate the rowwise mean when a maximum number of NA values is given for a set of columns using dplyr

Related Related

  1. 1

    How to calculate mean of grouped dataframe?

  2. 2

    Calculate creation number grouped by category in Postgres

  3. 3

    How to calculate mean values grouped on another column in Pandas

  4. 4

    How to calculate grand mean and standard deviation for the grouped table?

  5. 5

    how to calculate mean values of the past n years grouped by two variables

  6. 6

    Pandas: calculate mean for each row by cylcle number

  7. 7

    Count number of observations used to calculate the mean in SAS

  8. 8

    Pandas DataFrame: How to calculate the rolling mean of both grouped and shifted data in version >0.19

  9. 9

    Resample and select a given number of rows and calculate the mean, variance and confidence intervals?

  10. 10

    calculate mean and gradient of the a certain number of rows before a timestamp in another datset

  11. 11

    Calculate the mean number of days between dates per group

  12. 12

    How to calculate mean number of something per client in Teradata SQL?

  13. 13

    Order grouped scatterplot by mean

  14. 14

    Mean of grouped data

  15. 15

    Calculate mode grouped by a column

  16. 16

    Calculate value grouped by columns

  17. 17

    Calculate a grouped median in pyspark

  18. 18

    Calculate percentage of grouped values

  19. 19

    Calculate mean() of Nympy 2D-array grouped by values in a separate list with strings corresponding to each row in the 2D array

  20. 20

    calculate mean only when the number of values in each rows is higher then certain number in python pandas

  21. 21

    Applying .mean() to a grouped data with a condition

  22. 22

    dplyr mean columns grouped by name

  23. 23

    adding mean to grouped violin plot

  24. 24

    Calculate Group Mean and Overall Mean

  25. 25

    Pyspark calculate a field on a grouped table

  26. 26

    Calculate medians of rows in a grouped dataframe

  27. 27

    How to calculate difference on grouped df?

  28. 28

    Calculate number of

  29. 29

    Calculate the rowwise mean when a maximum number of NA values is given for a set of columns using dplyr

HotTag

Archive