get average column A based on a range of values in column B

孤者浪人 提交于 2019-12-08 21:34:43

问题


My dataframe has several columns as follows:

df1 <- data.frame(A = c(1,2,4), B=c(1,3,1), C=c(1,1,3))

I have two conditions to get average values for column A.

  • Condition 1: I want to get average of column A when B is 1, i.e. only row1 and row2 will be averaged.
  • Condition 2: I want to get average of column B when column A's values are larger than 1 but smaller than 3, i.e. only row 2 will be considered.

I know I can use filter to cut the dataframe to have column B = 1 only. However, I am unsure how to do it when I want the column B to be considered as a range within 1 and 3.

Are there any smarter ways to get the average values of column without cutting the dataframe into a smaller size first?


回答1:


You can do your subsetting in the same call to mean like so:

with(df1, mean(A[B == 1]))

with(df1, mean(B[A > 1 & A < 3]))



回答2:


You can combine two logical tests with &. So you could combine the B > 1 test with B < 3:

# Condition A:
mean(df1$A[df1$B==1])

# Condition B:
mean(df1$B[df1$A>1 & df1$A<3])


来源:https://stackoverflow.com/questions/6138052/get-average-column-a-based-on-a-range-of-values-in-column-b

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!