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
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.
Comments