Reputation:
How can I count the mean of Column 2 if Column 1 has a value of "UK"?
Column1 | Column2
-------------------
USA | 4.5
UK | 4.3
UK | 2.4
UK | 1.3
GERMANY | 4.4
FRANCE | 2.3
So I want to get mean of Columns 2 for UK.
Upvotes: 1
Views: 43
Reputation: 887511
We can subset the 'Column2' based on the 'Column1' value o 'UK' and get the mean
with(df1, mean(Column2[Column1 == 'UK']))
Or if we need to get the mean
of 'Column2' for all unique elements of 'Column1'
aggregate(Column2 ~ Column1, df1, mean)
Or with dplyr
library(dplyr)
df1 %>%
group_by(Column1) %>%
summarise(Column2 = mean(Column2, na.rm = TRUE))
Upvotes: 1