What function should I use to find the count of avg1 in a table that are larger than 0.2?
1
0
Entering edit mode
3.9 years ago
 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
R • 770 views
ADD COMMENT
0
Entering edit mode
  1. Try checking the names of the object before you execute the last command.
  2. Try sum(object[object$avg1>0.2,]$avg1) once you save the object
ADD REPLY
0
Entering edit mode
3.9 years ago

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

ADD COMMENT
0
Entering edit mode

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?

ADD REPLY
0
Entering edit mode

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)
ADD REPLY
0
Entering edit mode

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
ADD REPLY

Login before adding your answer.

Traffic: 2704 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6