I need the count of all values in column avg1 that are greater than 0.2 Any idea how to do that? other than using sum function?
What function should I use to find the count of avg1 in a table that are larger than 0.2?
bat_02 <- Batting %>% filter(yearID %in% 1999:2001) %>%
mutate(pa = AB + BB, singles = (H - X2B - X3B - HR)/pa, bb = BB/pa) %>%
filter(pa >= 100) %>%
select(playerID, singles, bb)
bat_02 %>%
group_by(playerID) %>%
summarize(avg1 = mean(singles))
# A tibble: 580 x 2
playerID avg1
<chr> <dbl>
1 abbotje01 0.169
2 abbotku01 0.143
3 abernbr01 0.178
4 abreubo01 0.153
5 agbaybe01 0.162
6 alexama02 0.166
7 alfoned01 0.161
8 alicelu01 0.168
9 allench01 0.165
10 alomaro01 0.186
# ... with 570 more rows
> sum(avg1 > 0.2)
Error: object 'avg1' not found
• 1,335 views
•
link
1 answer
You have no object called avg1 in your workspace. Please go back and explore the reason why. Check the input and output of every command, and also learn ways how to look inside the contents of your objects via, for example, head() and tail(), and learn about index sub-setting of data-frames and -matrices, and also tibbles.
A hint: 'avg1' seems to just be a column name in your bat_02 object
Kevin
• 0 views
•
link
• 0 views
•
link
Please go carefully through each of your commands and study the input and output. You are not saving the output of your following command (it is just being printed to screen):
bat_02 %>%
group_by(playerID) %>%
summarize(avg1 = mean(singles))
You probably need something like this:
bat_02_summarised <- bat_02 %>%
group_by(playerID) %>%
summarize(avg1 = mean(singles))
sum(bat_02_summarised$avg1 > 0.2)
• 0 views
•
link
sum(bat_02_summarised$avg1 > 0.2) would give the count of Trues
eg.
> sum(iris$Sepal.Length)
[1] 876.5
> sum(iris[iris$Sepal.Length > 5.1,]$Sepal.Length)
[1] 677.4
> sum(iris$Sepal.Length>5.1)
[1] 109
• 0 views
•
link
Log in to answer this question.
sum(object[object$avg1>0.2,]$avg1)once you save the object