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.
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?
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]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With